Android kitkat Image Selection From Gallery Issue

≯℡__Kan透↙ 提交于 2019-12-23 05:32:29

问题


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.


回答1:


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

getContentResolver().takePersistableUriPermission(originalUri, takeFlags);

try to catch exception and should work.



来源:https://stackoverflow.com/questions/22144042/android-kitkat-image-selection-from-gallery-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!