英文:
How to get real path of capture image and gallery image in android in android 10 and above
问题
以下是翻译好的部分:
获取捕获图像和相册图像的真实路径,以下代码在我的情况下运行得非常好。
private String getRealPathFromURI(Uri contentUri) {
String result = null;
try{
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
cursor.close();
}catch(Exception e){
Log.i("error_getting_path",e.getMessage());
}
return result;
}
但在 Android 10 及以上的设备上,我无法获取捕获和相册图像的路径。
因此,在 Android 10 及以上的设备上,我的应用无法使用此功能。
请建议在所有 Android 设备上获取路径的最佳方法。
英文:
for getting the real path of capture image and gallery image the following code work perfectly fine for me.
private String getRealPathFromURI(Uri contentUri) {
String result = null;
try{
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
cursor.close();
}catch(Exception e){
Log.i("error_getting_path",e.getMessage());
}
return result;
}
but in android 10 and above device i am not able to get the path of capture and gallery image .
so my app is not working in android 10 and above for this feature.
please suggest the best way to get path in all android devices.
答案1
得分: 1
private static String getRealPathFromURI(Uri uri) {
Uri returnUri = uri;
Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String name = (returnCursor.getString(nameIndex));
File file = new File(context.getFilesDir(), name);
try {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
inputStream.close();
outputStream.close();
Log.e("File Path", "Path " + file.getAbsolutePath());
} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
return file.getAbsolutePath();
}
英文:
private static String getRealPathFromURI(Uri uri) {
Uri returnUri = uri;
Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex( OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
String name = (returnCursor.getString(nameIndex));
File file = new File(context.getFilesDir(), name);
try {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
inputStream.close();
outputStream.close();
Log.e("File Path", "Path " + file.getAbsolutePath());
} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
return file.getAbsolutePath();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论