retrieve absolute path when select image from gallery kitkat android

前端 未结 4 1337
说谎
说谎 2020-12-01 07:59

As I am supporting my app to Kitkat version, now in this the way of retrieve file from gallery was different.

I have preferred this Android Gallery on KitKat returns

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 08:42

    Pratik's solution helped me alot. Following is the version which works for me in Kitkat 4.4.2. Three things which i changed are
    1) Using content resolver to get the path
    2) originalUri.getLastPathSegment().split(":")[1] gives me index out of bound so i'm using index 0 instead.Its been working so far
    3) Removed takeflags and check for freshest data as we are filtering the cursor with id.

    try {
            Uri originalUri = data.getData();
            String pathsegment[] = originalUri.getLastPathSegment().split(":");
            String id = pathsegment[0];
            final String[] imageColumns = { MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;
    
            Uri uri = getUri();
            Cursor imageCursor = activity.getContentResolver().query(uri, imageColumns,
                                    MediaStore.Images.Media._ID + "=" + id, null, null);
    
            if (imageCursor.moveToFirst()) {
                value = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
    
        } catch (Exception e) {
            Toast.makeText(activity, "Failed to get image", Toast.LENGTH_LONG).show();
        }
    

提交回复
热议问题