Search code examples
androidandroid-imageandroid-galleryactivitynotfoundexception

Get Image from gallery cause no activity found error in activity resulte


I try to open the gallery and let user to choose image from gallery it's work on some android but not all in my tablet (4.2.2) it's work just fine but my friend (4.1.2) it's cause ActivityNotFound Exeption.

the code to open gallery:

Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    try
                    {
                        startActivityForResult(i, RESULT_LOAD_IMAGE);
                    }
                    catch (ActivityNotFoundException e) 
                    {
                        e.printStackTrace();
                    }

And This is ActivityResult code:

@Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
}

Solution

  • try:-

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);