当前位置: 首页 > 面试题库 >

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

颜均
2023-03-14
问题内容

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

基于本教程。

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

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

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

我这样检索它的路径

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

并最终得到这个典型的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/Yfile:///storage/sdcard0/Pictures/X.jpg


问题答案:

这样的事情对您有用吗?这是查询内容解析器以查找为该内容条目存储的文件路径数据

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



 类似资料:
  • 我在从我的uri中检索位图时遇到困难。我试图通过Intent打开相机后从相机中获取图像。 记录时的ImageUri是 content://com.example/external_files/Pictures/CustomFolder/15122017_photo_115344.jpg 打开相机并拍照后,将调用我的onActivityResult。因为我用的是MediaStore。EXTRA_OU

  • 我有些设备有问题。我不能在任何设备上复制它,但我有相当多的崩溃报告,由一些用户报告。 这是一个例外: Context.GetContentResolver().Delete(mediastore.files.GetContentURI(“external”),mediastore.files.fileColumns.data+“=?”,new String[]{path}); 之后,我在文件的父目

  • 您可以使用CSS在网页上打印时更改网页的外观。 您可以为屏幕版本指定一种字体,为打印版本指定另一种字体。 您在前面的章节中看到了@media规则。 此规则允许您为不同的媒体指定不同的样式。 因此,您可以为屏幕和打印机定义不同的规则。 下面的示例为屏幕和打印指定了不同的字体系列。 下一个CSS对于屏幕和打印机都使用相同的字体大小。 <style type = "text/css"> <!--

  • Web文档可以由语音合成器呈现。 CSS2允许您将特定的声音样式功能附加到特定的文档元素。 文件的听觉呈现主要由视障者使用。 可以通过听觉呈现而不是视觉呈现来访问文档的一些情况如下。 学习阅读 Training 车辆中的Web访问 家庭娱乐 工业文件 医疗文件 当使用听觉属性时,画布由三维物理空间(声音环绕)和时间空间(可以在其他声音之前,期间和之后指定声音)组成。 CSS属性还允许您改变合成语音

  • Media The Media object provides the ability to record and play back audio files on a device. var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]); NOTE: The current implementation do

  • This plugin provides the ability to record and play back audio files on a device. NOTE: The current implementation does not adhere to a W3C specification for media capture, and is provided for conveni