【问题标题】:How to get the path of image android?如何获取图像android的路径?
【发布时间】:2020-02-20 09:22:53
【问题描述】:

我正在从我的应用中选择画廊的图片, 使用

ACTION_GET_CONTENT

现在我想要这张图片的路径:

但我是这样理解的:

content://com.android.providers.media.documents/document/image%3A5793

如何获取路径?

我试过这个来获取路径:

Uri uri = data.getData();
Log.e("Path is: "+uri);

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    答案是不要尝试获取路径,因为在 Android 10 及更高版本中您将无法获取或使用它。

    您可以获得FileDescriptor 或输入/输出流,可用于大多数需要访问文件内容的方法。

    更多详情请见https://developer.android.com/training/data-storage/,因为它是一张图片,所以最好使用媒体商店。

    【讨论】:

      【解决方案2】:

      试试这个:

      Uri uri = data.getData();
              String picturePath = getPath( getActivity( ).getApplicationContext( ), uri);
              Log.e("Picture Path", picturePath);
      
      public static String getPath( Context context, Uri uri ) {
          String result = null;
          String[] proj = { MediaStore.Images.Media.DATA };
          Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
          if(cursor != null){
              if ( cursor.moveToFirst( ) ) {
                  int column_index = cursor.getColumnIndexOrThrow( proj[0] );
                  result = cursor.getString( column_index );
              }
              cursor.close( );
          }
          if(result == null) {
              result = "Not found";
          }
          return result;
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多