Skip to content Skip to sidebar Skip to footer

Choose Picture Intent Causes Nullpointer Exception

public void uploadpicture(View view) { Intent intent = new Intent(); intent.setType('image/*'); intent.setAction(Intent.ACTION_GET_CONT

Solution 1:

From your log it looks like you are trying to pick an image from Picasa folder dat=content://com.android.sec.gallery3d.provider/picasa/item/5147009501910019474, and these must be handled differently, because data in MediaStore.Images.Media.DATA column will be null for them. You can see how to handle picking images from Picasa correctly at this link:

http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/

Only you also need to handle URIs that start with "content://com.sec.android.gallery3d" (I see this on Samsung S3) so change

if (selectedImage.toString().startsWith("content://com.google.android.gallery3d"))

to

if (_selectedImage.toString().startsWith("content://com.google.android.gallery3d") 
    || selectedImage.toString().startsWith("content://com.sec.android.gallery3d"))

I'm not sure if there are any more special cases (probably yes).

Solution 2:

I ran into this same problem and tried implementing this based on the website user1682516 recommended. I was banging my head against the wall because my Picasa content URLs didn't quite match: mine looked like "com.sec.android.gallery3d.provider" and not "com.android.gallery3d.provider" but didn't seem to work whether I altered the URL or not.

Then I felt dumb when I eventually realized that the library I use to load remote images for ImageViews in my app, https://github.com/nostra13/Android-Universal-Image-Loader, takes care of this for me. I can just call ImageLoader.getInstance().loadImage() with the appropriate parameters and get a Bitmap in the callback. I don't need to worry about whether it is a local image, a remote Picasa image, or an ordinary remote image--that's taken care of for me. Wish I had figured that out hours ago!

Solution 3:

Tested on Samsung SM-A520F. Works if you want to pick only local images from your gallery:

funuploadpicture(view: View?) {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) // Restrict choice
        startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE)
    }

Solution 4:

Tested Code here

publicclassSelectPhotoActivityextendsActivity {

privatestaticfinalintSELECT_PICTURE=1;
private String selectedImagePath="";

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intentintent=newIntent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(intent, SELECT_PICTURE); 
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursorcursor= managedQuery(uri, projection, null, null, null);
    intcolumn_index= cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            UriselectedImageUri= data.getData();
            selectedImagePath = getPath(selectedImageUri);
           // here you can set the image
            }
    }
}

}

Post a Comment for "Choose Picture Intent Causes Nullpointer Exception"