简体   繁体   中英

Android error intent pick image from gallery depending on path

I have a function which tries to get the image from the image path, it works depending on were the image is.

For some reason, it works fine from this location

/storage/extSdCard/DCIM/100MEDIA/IMAG0082.jpg

but these locations does not work

/storage/extSdCard/DCIM/Camera/20150315_133312.jpg
/storage/emulated/0/Pictures/20150825_161105.jpg

It results in a blank thumbnail and an error when i try to upload the image from file path.

Here is my Intent for picking the photo:

//Camera Application for existing Photo
public void btnExistingPhotoClicked(View v) {
    //invoke the intent to get an image from the phone
    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(pickPhoto, 1);//one can be replaced with any action code


}

And here is the actual onActivityResult which picks it up, put it in a thumbnail and then display the imagepath in a Textview

case 1:
                if(resultCode == RESULT_OK){

                    //function for a selected image
                    Uri selectedImage = imageReturnedIntent.getData();

                    mImageView.setImageURI(selectedImage);

                    Toast.makeText(this, mImageView.toString(), Toast.LENGTH_LONG).show();
                    Log.d("MainActivity", mImageView.toString());

                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imgurl = cursor.getString(columnIndex);
                    cursor.close();

                    //The imageview is set, this is the small preview in the app
                    TextView tximgurl = (TextView) findViewById(R.id.txtImgUrl);
                    tximgurl.setText(imgurl);
                    Log.d("MainActivity", imgurl.toString());
                    //Toast.makeText(this, imgurl, Toast.LENGTH_LONG).show();


                }
                break;

Only use this.

Intent pickPhoto = new Intent(Intent.ACTION_PICK) ;
pickPhoto.setType("image.*/");

And add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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