简体   繁体   English

如何在Android中将`content:// media / external / images / media / Y`转换为`file:/// storage / sdcard0 / Pictures / X.jpg`?

[英]how to convert `content://media/external/images/media/Y` to `file:///storage/sdcard0/Pictures/X.jpg` in android?

I'm trying to upload image to Google Drive from my android app, 我正在尝试从我的Android应用程序将图像上传到Google云端硬盘,

based on this tutorial . 基于本教程

When I debug their sample project I see a typical fileUri = 当我调试他们的示例项目时,我看到一个典型的fileUri =

file:///storage/sdcard0/Pictures/IMG_20131117_090231.jpg

In my app I want to upload an existing photo. 在我的应用中,我想上传现有照片。

I retrieve its path like that 我这样检索它的路径

     private void GetAnyImage()
        {
            File dir = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Pictures/Screenshots"); 
                              // --> /storage/sdcard0/Pictures/Screenshots

            Log.d("File path ", dir.getPath());
            String dirPath=dir.getAbsolutePath();
            if(dir.exists() && dir.isDirectory()) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent.setType("image/*");
                startActivityForResult(intent,REQUEST_ID);
            }  
        }

and eventually get this typical fileUri = content://media/external/images/media/74275 并最终得到这个典型的文件Uri = content://media/external/images/media/74275

however when running this line of code 但是,运行此行代码时

  private void saveFileToDrive() {

    //  progressDialog = ProgressDialog.show(this, "", "Loading...");

    Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          // File's binary content
          java.io.File fileContent = new java.io.File(fileUri.getPath());
          FileContent mediaContent = new FileContent("image/jpeg", fileContent);

      // File's metadata.
      File body = new File();
      body.setTitle(fileContent.getName());
      body.setMimeType("image/jpeg");

      File file = service.files().insert(body, mediaContent).execute();

I get an error: 我收到一个错误:

java.io.FileNotFoundException: /external/images/media/74275: open failed: ENOENT (No such file or directory)

how can I solve this? 我该如何解决?

how to convert content://media/external/images/media/Y to file:///storage/sdcard0/Pictures/X.jpg ? 如何将content://media/external/images/media/Yfile:///storage/sdcard0/Pictures/X.jpg

Will something like this work for you? 这样的事情对您有用吗? What this does is query the content resolver to find the file path data that is stored for that content entry 这是查询内容解析器以查找为该内容条目存储的文件路径数据

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

This will end up giving you an absolute file path that you can construct a file uri from 这最终将为您提供一个绝对的文件路径,您可以从中构建文件uri

If you just want the bitmap, This too works 如果您只想要位图,这也可以

InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
if( inputStream != null ) inputStream.close();

sample uri : content://media/external/images/media/12345 样本uri:content:// media / external / images / media / 12345

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 2020年如何将文件保存到媒体目录中的外部存储? - How to save a file to external storage in the media directory in 2020? 每次打开应用程序时,在Android sdcard0中动态保存.txt文件 - Saving a .txt file dynamically in Android sdcard0 when everytime applicaiion opens 如何在 android 11 上使用没有 READ_EXTERNAL_STORAGE 的 File("/sdcard/myfile")? - How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11? 如何使用适用于Android的API转换媒体文件格式 - How to convert media file format using API for Android “resolveUri在错误的位图uri上失败:content:// media / external / images / media / 49644”我无法查看图像 - “resolveUri failed on bad bitmap uri: content://media/external/images/media/49644” Im unable to view the image Android 11:主目录(无效)不允许内容://媒体/外部/文件允许的目录是[下载,文档] - Android 11: Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, Documents] 如何从 Android 本地存储中获取媒体文件(图像、视频、音频)? - How to fetch media files (Images,videos,audio) from Android local storage? 当您的路径为`/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg`时,删除Android中的文件 - Delete a file in Android when you have a path of form `/storage/1018-2710/Pictures/oLvCVPZrNxk.jpg` 如何从 Android 的外部存储中获取图像? - How to fetch images from External Storage in Android? Android Media文件长度 - Android Media File Length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM