I have to get image from internal storage of mobile device and upload to server. If I select image from sdcard it working fine, while if I select image from internal storage it show error, I am not able to get this error. Here is logcat output -
07-26 10:32:24.868: W/System.err(24051): java.io.FileNotFoundException: /document/primary:Pictures/Screenshots/Screenshot_20160712-172722.png: open failed: ENOENT (No such file or directory)
I am using this code to get image
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
1);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
OnActivityResult code
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// 1
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
try {
// SDK < API11
if (Build.VERSION.SDK_INT < 11)
realPath_1 = RealPathUtil
.getRealPathFromURI_BelowAPI11(getActivity(),
data.getData());
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath_1 = RealPathUtil.getRealPathFromURI_API11to18(
getActivity(), data.getData());
// SDK > 19 (Android 4.4)
else
realPath_1 = RealPathUtil.getRealPathFromURI_API19(
getActivity(), data.getData());
Log.e("", "Build Version : " + Build.VERSION.SDK_INT);
Log.e("", "Get Data Get Path : " + data.getData().getPath());
if (realPath_1.equalsIgnoreCase("")) {
realPath_1 = data.getData().getPath().toString();
}
Log.e("", "Real Path 1 : " + realPath_1);
file_1 = realPath_1.substring(
realPath_1.lastIndexOf('/') + 1,
realPath_1.length());
Log.i("File Name 1 ", file_1);
txt_file_name_1.setText(file_1);
} catch (Exception e) {
Uri selected = data.getData();
realPath_1 = selected.getPath();
file_1 = realPath_1.substring(
realPath_1.lastIndexOf('/') + 1,
realPath_1.length());
Log.i("File Name 1 ", file_1);
txt_file_name_1.setText(file_1);
}
}
}}
Here is class named - RealPathUtil to get path of images. My
android:minSdkVersion="14" android:targetSdkVersion="23"
and called this method from class RealPathUtil
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
Please help and thanks in advance.
Try this:
boolean isKitKat = false;
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
Code for show file chooser function
private void showFileChooser() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
isKitKat = true;
startActivityForResult(Intent.createChooser(intent, "Select file"), 1);
} else {
isKitKat = false;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"), 1);
}
}
OnActivity Result code :
@TargetApi(19)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (data != null && data.getData() != null && resultCode == getActivity().RESULT_OK) {
boolean isImageFromGoogleDrive = false;
Uri uri = data.getData();
if (isKitKat && DocumentsContract.isDocumentUri(getActivity(), uri)) {
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
realPath_1 = Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
Pattern DIR_SEPORATOR = Pattern.compile("/");
Set<String> rv = new HashSet<>();
String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
if (TextUtils.isEmpty(rawExternalStorage)) {
rv.add("/storage/sdcard0");
} else {
rv.add(rawExternalStorage);
}
} else {
String rawUserId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
rawUserId = "";
} else {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String[] folders = DIR_SEPORATOR.split(path);
String lastFolder = folders[folders.length - 1];
boolean isDigit = false;
try {
Integer.valueOf(lastFolder);
isDigit = true;
} catch (NumberFormatException ignored) {
}
rawUserId = isDigit ? lastFolder : "";
}
if (TextUtils.isEmpty(rawUserId)) {
rv.add(rawEmulatedStorageTarget);
} else {
rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
}
}
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
String[] temp = rv.toArray(new String[rv.size()]);
for (int i = 0; i < temp.length; i++) {
File tempf = new File(temp[i] + "/" + split[1]);
if (tempf.exists()) {
realPath_1 = temp[i] + "/" + split[1];
}
}
}
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(contentUri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
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;
}
String selection = "_id=?";
String[] selectionArgs = new String[]{split[1]};
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(contentUri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.google.android.apps.docs.storage".equals(uri.getAuthority())) {
isImageFromGoogleDrive = true;
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
realPath_1 = uri.getPath();
}
try {
Log.d(TAG, "Real Path 1 : " + realPath_1);
file_1 = realPath_1.substring(realPath_1.lastIndexOf('/') + 1, realPath_1.length());
Log.i("File Name 1 ", file_1);
} catch (Exception e) {
e.printStackTrace();
}
}
}}
Here file_1 = get image name and realPath_1 show full path of image from internal or external storage.
Happy to help :)