【问题标题】:Easy and universal way to get filename shared with ACTION_SEND与 ACTION_SEND 共享文件名的简单通用方法
【发布时间】:2015-02-01 03:08:24
【问题描述】:

我正在接收来自不同应用程序的使用默认共享机制 (ACTION_SEND) 发送的意图。

如果是文件,我需要复制它。
我不需要文件的完整路径,因为我可以直接用流打开它。

InputStream input = getContentResolver().openInputStream(fileUri);

但我需要知道文件名才能正确保存文件。

是否有任何简单通用的方法来获取共享文件的文件名而不注意内容类型(可以是图库中的图像、视频、下载中的文件)?


或者我应该使用这样的东西从 uri 中获取完整路径,然后从中获取文件名?

Get real path from URI, Android KitKat new storage access framework [duplicate]
Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT


我也看到了这篇文章(A Uri Is Not (Necessarily) a File),里面说:

您可以对此类 Uri 值执行以下操作:

  • 通过 Uri 上的 query() 找出它们的 DISPLAY_NAME 和 SIZE — OpenableColumns(同样,使用 ContentResolver,或者理论上是 游标加载器)

有可能吗?我该怎么做?

【问题讨论】:

    标签: android android-intent android-sharing


    【解决方案1】:

    根据 MediaStore 接口,确实传递了 DISPLAY_NAME 列。

    Cursor cursor = getContentResolver().query(fileUri, projection, null, null, null);
    

    投影包含DISPLAY_NAME

    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        // get your file name
    }
    

    我还没有测试过,但它应该可以工作 ;-)

    【讨论】:

      猜你喜欢
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      相关资源
      最近更新 更多