如何在 Android 10 及更高版本中获取捕获图像和图库图像的真实路径。

huangapple go评论87阅读模式
英文:

How to get real path of capture image and gallery image in android in android 10 and above

问题

以下是翻译好的部分:

获取捕获图像和相册图像的真实路径,以下代码在我的情况下运行得非常好。

  1. private String getRealPathFromURI(Uri contentUri) {
  2. String result = null;
  3. try{
  4. String[] proj = { MediaStore.Images.Media.DATA };
  5. CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
  6. Cursor cursor = loader.loadInBackground();
  7. int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  8. cursor.moveToFirst();
  9. result = cursor.getString(column_index);
  10. cursor.close();
  11. }catch(Exception e){
  12. Log.i("error_getting_path",e.getMessage());
  13. }
  14. return result;
  15. }

但在 Android 10 及以上的设备上,我无法获取捕获和相册图像的路径。
因此,在 Android 10 及以上的设备上,我的应用无法使用此功能。
请建议在所有 Android 设备上获取路径的最佳方法。

英文:

for getting the real path of capture image and gallery image the following code work perfectly fine for me.

  1. private String getRealPathFromURI(Uri contentUri) {
  2. String result = null;
  3. try{
  4. String[] proj = { MediaStore.Images.Media.DATA };
  5. CursorLoader loader = new CursorLoader(requireActivity(), contentUri, proj, null, null, null);
  6. Cursor cursor = loader.loadInBackground();
  7. int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  8. cursor.moveToFirst();
  9. result = cursor.getString(column_index);
  10. cursor.close();
  11. }catch(Exception e){
  12. Log.i("error_getting_path",e.getMessage());
  13. }
  14. return result;
  15. }

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

  1. private static String getRealPathFromURI(Uri uri) {
  2. Uri returnUri = uri;
  3. Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
  4. int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
  5. returnCursor.moveToFirst();
  6. String name = (returnCursor.getString(nameIndex));
  7. File file = new File(context.getFilesDir(), name);
  8. try {
  9. InputStream inputStream = context.getContentResolver().openInputStream(uri);
  10. FileOutputStream outputStream = new FileOutputStream(file);
  11. int read = 0;
  12. int maxBufferSize = 1 * 1024 * 1024;
  13. int bytesAvailable = inputStream.available();
  14. int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  15. final byte[] buffers = new byte[bufferSize];
  16. while ((read = inputStream.read(buffers)) != -1) {
  17. outputStream.write(buffers, 0, read);
  18. }
  19. inputStream.close();
  20. outputStream.close();
  21. Log.e("File Path", "Path " + file.getAbsolutePath());
  22. } catch (Exception e) {
  23. Log.e("Exception", e.getMessage());
  24. }
  25. return file.getAbsolutePath();
  26. }
英文:
  1. private static String getRealPathFromURI(Uri uri) {
  2. Uri returnUri = uri;
  3. Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
  4. int nameIndex = returnCursor.getColumnIndex( OpenableColumns.DISPLAY_NAME);
  5. returnCursor.moveToFirst();
  6. String name = (returnCursor.getString(nameIndex));
  7. File file = new File(context.getFilesDir(), name);
  8. try {
  9. InputStream inputStream = context.getContentResolver().openInputStream(uri);
  10. FileOutputStream outputStream = new FileOutputStream(file);
  11. int read = 0;
  12. int maxBufferSize = 1 * 1024 * 1024;
  13. int bytesAvailable = inputStream.available();
  14. int bufferSize = Math.min(bytesAvailable, maxBufferSize);
  15. final byte[] buffers = new byte[bufferSize];
  16. while ((read = inputStream.read(buffers)) != -1) {
  17. outputStream.write(buffers, 0, read);
  18. }
  19. inputStream.close();
  20. outputStream.close();
  21. Log.e("File Path", "Path " + file.getAbsolutePath());
  22. } catch (Exception e) {
  23. Log.e("Exception", e.getMessage());
  24. }
  25. return file.getAbsolutePath();
  26. }

huangapple
  • 本文由 发表于 2020年9月23日 19:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64027218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定