Skip to content Skip to sidebar Skip to footer

Android Getting Pictures From Photo's Gallery - Error On Some Devices

I have a function on my app which I open the photo's gallery and select an image to show in a ImageView. The weird thing is, on my Samsung Galaxy S5, the code work perfectly, but w

Solution 1:

publicstaticfinalint GALLERY_INTENT_REQUEST_CODE = 0x000005;

To open Gallery & get path of selected image use following code :

IntentphotoPickerIntent=newIntent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 photoPickerIntent.setType("image/*");
 photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        
 mActPanelFragment.startActivityForResult(photoPickerIntent, ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE);

After that you get path in onActivityResult() method

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
(requestCode == ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
try {
            StringimagePath= getFilePath(data);
            // TODO: Here you set data to preview screen
    }catch(Exception e){}
}
}

private String getFilePath(Intent data) {
    String imagePath;
    UriselectedImage= data.getData();
    String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

    intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
    imagePath = cursor.getString(columnIndex);
    cursor.close();

    return imagePath;

}

Solution 2:

There is no requirement that ACTION_GET_CONTENT return to you a Uri from the MediaStore. Even if it does, there is no requirement that your code will give you a path to a file that you have read access to. In your case, imgString is null, which is not terribly surprising.

Instead, get rid of all of this (including whatever wd is) and use an image-loading library, like Picasso, that can just take the Uri to the image and handle all the loading and resizing for you (on a background thread) and update your ImageView.

If you insist upon doing this yourself, fork a background thread, then use ContentResolver and methods like openInputStream() and getType(), along with BitmapFactory.

Post a Comment for "Android Getting Pictures From Photo's Gallery - Error On Some Devices"