如何在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?

查看:6713
本文介绍了如何在Android中将`content://media/external/images/media/Y`转换为`file:///storage/sdcard0/Pictures/X.jpg`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像从我的android应用程序上传到Google云端硬盘,

I'm trying to upload image to Google Drive from my android app,

基于此教程.

当我调试他们的示例项目时,我看到一个典型的fileUri =

When I debug their sample project I see a typical fileUri =

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

在我的应用中,我想上传现有照片.

In my app I want to upload an existing photo.

我这样检索路径

     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);
            }  
        }

并最终获得此典型文件Uri = content://media/external/images/media/74275

and eventually get this typical fileUri =content://media/external/images/media/74275

但是运行此行代码

  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();

我得到一个错误:

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

我该如何解决?

如何将content://media/external/images/media/Y转换为file:///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();
        }
    }
}

这最终将为您提供一个绝对的文件路径,您可以从中构建文件uri

This will end up giving you an absolute file path that you can construct a file uri from

这篇关于如何在Android中将`content://media/external/images/media/Y`转换为`file:///storage/sdcard0/Pictures/X.jpg`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆