简体   繁体   English

从图库中选择图像时崩溃

[英]Crash on Selecting Image from the Gallery

Here is the code, when selecting an image the app abruptly crashes :/ Please help , I cant progress further without this error being fixed. 这是代码,选择图片时应用突然崩溃:/ 请帮助 ,如果不解决此错误,我将无法继续前进。

Manifest 表现

<activity
        android:launchMode="singleTop"
        android:name=".FoundMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.guruguru2.lostnfound.FOUNDMENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

ImageView XML ImageView XML

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.71"
    android:src="@drawable/abc_list_divider_mtrl_alpha" />

.java file .java文件

Button pickImageButton = (Button)findViewById(R.id.pick_image_button);
private static final int PICK_IMAGE = 100;
private ImageView imageView2;
pickImageButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            openGallery();
         }
      });




}
private void openGallery() {                     //opens the gallery
      Intent gallery = 
         new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
      startActivityForResult(gallery, PICK_IMAGE);
   }
@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
         Uri imageUri = data.getData();
         imageView2.setImageURI(imageUri);
      }
   }

LogCat LogCat

E/AndroidRuntime(1083): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/16 }} to activity {com.guruguru2.lostnfound/com.guruguru2.lostnfound.FoundMenu}: java.lang.NullPointerException 

The main goal here is to select an image from the gallery, and simply show it. 这里的主要目标是从图库中选择图像,然后简单地显示它。 I can post more logcat if needed, there is a lot more, this error seemed the most fatal though. 如果需要,我可以发布更多logcat,还有更多,这个错误似乎是最致命的。

You might try to change the intent from INTERNAL_CONTENT_URI to EXTERNAL_CONTENT_URI. 您可以尝试将意图从INTERNAL_CONTENT_URI更改为EXTERNAL_CONTENT_URI。

Check this stackoverflow url for further information on this issue: Android get image from gallery into ImageView 检查此stackoverflow网址以获取有关此问题的更多信息: Android将图像从库中获取到ImageView中

Here is the code, when selecting an image the app abruptly crashes 这是代码,选择图片时应用突然崩溃

You forgot to set the view to the ImageView do it as follows : 您忘记将视图设置为ImageView请执行以下操作:

imageView2 = (ImageView)findViewById(R.id.ImageView2);

Then you are able to set the image to your ImageView , it doesn't mean that it solves your problem, but it's the MAIN error in your code as I can see. 然后,您可以将图像设置为ImageView ,这并不意味着它可以解决您的问题,但是正如我所看到的,这是代码中的主要错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM