0

I have been trying to pick image from the gallery of Nexus devices..Since the Storage framework has been changed in kitkat 4.4 its been a headache for me to implement this functionality..I have referred to this link retrieve absolute path when select image from gallery kitkat android

I am getting the path of the file in the gallery..and also decode the image and set it into the imageView..but as soon I do this..My app closes suddenly and again reconnects to the server and tells me to log in again..Could anyone please tell me the reasons for such happenings..is there any permission to be added or some more piece of code..

this is the code for getting the Imagepath..

    Uri originalUri = data.getData();

            final int takeFlags = data.getFlags()
                                & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            // Check for the freshest data.
            getContentResolver().takePersistableUriPermission(originalUri, takeFlags);

            String id = originalUri.getLastPathSegment().split(":")[1]; 
            final String[] imageColumns = {MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;

            Uri uri = getUri();

            @SuppressWarnings("deprecation")
            Cursor imageCursor = managedQuery(uri, imageColumns,
                  MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);

            if (imageCursor.moveToFirst())
            {
                picturepath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            Bitmap b=decodeSampledBitmapFromFile(picturepath, 175, 175);
            imgToBeUploaded.setImageBitmap(b);
            Toast.makeText(StartingActivity.this, picturepath, Toast.LENGTH_SHORT).show();

Thanks and Regards.

Community
  • 1
  • 1
AndroidMech
  • 1,576
  • 2
  • 18
  • 30

1 Answers1

0

Most probably you are getting java.lang.SecurityException because of this line

getContentResolver().takePersistableUriPermission(originalUri, takeFlags);

try to catch exception and should work.

duggu
  • 35,841
  • 11
  • 112
  • 110
Tintineg
  • 1
  • 1