2

I use this example to get image path. Every thing OK when I get small image path, but when I want to get huge image path my application crush. I don't know why this happen because I don't use ImageView to show image from selected path.

My code is here to open gallery and select image:

 selectImgBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, 0); //urlTV.setText(getMainPath()); } }); 

And the second to show path:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && data != null){ String realPath; if (Build.VERSION.SDK_INT < 19){ realPath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData()); } else { realPath = RealPathUtil.getRealPathFromURI_API19(this, data.getData()); } setMainPath(realPath); /*setFile(realPath); setToTextViews(Build.VERSION.SDK_INT, data.getData().getPath(), realPath);*/ urlTV.setText(getMainPath()); } } 

Thanks!

  • Caused by: java.lang.IllegalArgumentException: Not a document: content://media/external/images/media/32257 at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:629) at com.example.murager.httpclientapp.classes.RealPathUtil.getRealPathFromURI_API19(RealPathUtil.java:19) at com.example.murager.httpclientapp.activities.MainActivity.onActivityResult(MainActivity.java:98) at android.app.Activity.dispatchActivityResult(Activity.java:5456) at android.app.ActivityThread.deliverResults(ActivityThread.java:3549)             at android.app.ActivityThread.handleSendResult(ActivityThread.java:3596)             at android.app.ActivityThread.access$1300(ActivityThread.java:151)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)             at android.os.Handler.dispatchMessage(Handler.java:110)             at android.os.Looper.loop(Looper.java:193)             at android.app.ActivityThread.main(ActivityThread.java:5292)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)             at dalvik.system.NativeStart.main(Native Method)

3

2 Answers 2

8

Use this class to get image path in onActivityResult

public final class GetFilePathFromDevice { /** * Get file path from URI * * @param context context of Activity * @param uri uri of file * @return path of given URI */ public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[]{split[1]}; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } } 

It Automatically checks whether device OS is lollipop or above or lower than lollipop and according to it gives image path. Check if it works in your case.

You can use this class like this.

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && data != null){ String realPath = GetFilePathFromDevice.getPath(this, data.getData()); setMainPath(realPath); urlTV.setText(getMainPath()); } } 

I hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I add your code to get image path but when open gallery it crashes and message as "Unfortunately document has stopped.",Any issue? [Device OS = "Kitkat"]
0

Error is because of API Level. The code you used is compatible for specific API levels only. Try this.

if (Build.VERSION.SDK_INT <19){ Intent intent = new Intent(); intent.setType("image/jpeg"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED); }else{ Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/jpeg"); startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) return; if (null == data) return; Uri originalUri = null; if (requestCode == GALLERY_INTENT_CALLED) { originalUri = data.getData(); } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) { 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); } loadSomeStreamAsynkTask(originalUri); 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.