0

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
    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

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

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.

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
guruguru32
  • 41
  • 1
  • 1
  • 5

2 Answers2

0

You might try to change the intent from INTERNAL_CONTENT_URI to EXTERNAL_CONTENT_URI.

Check this stackoverflow url for further information on this issue: Android get image from gallery into ImageView

Community
  • 1
  • 1
0

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 :

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.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148