【问题标题】:Delete the file after obtaining uri获取uri后删除文件
【发布时间】:2016-08-28 17:20:16
【问题描述】:

我使用下面的方法从位图中获取uri

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    Uri storeUri = Uri.parse(path);
    File file = new File(path);
    Boolean b=file.delete();
    Log.d("DeletepathStoreUri",b.toString());
    inImage.recycle();
    return storeUri;
}

但这种方法总是将文件保存到图片目录和日志中,总是给出错误消息。

我哪里做错了?

【问题讨论】:

    标签: android file bitmap uri


    【解决方案1】:

    但是这种方法总是将文件保存到图片目录中

    它将字节存储在MediaStore 想要的任何位置,以它想要的任何格式,以它想要的任何质量。恕我直言,没有人应该使用insertImage()

    和日志,总是给出错误的信息

    这是因为 insertImage() 在大多数现代 Android 设备上不应返回 file Uri。它应该返回一个contentUri。没有可供您删除的文件系统路径。

    我哪里做错了?

    您的代码,即使它“有效”,也将毫无用处。您要求MediaStore 将字节写出某处,然后您转身删除这些字节。这完全没有意义。

    【讨论】:

    猜你喜欢
    • 2011-10-12
    • 2014-06-06
    • 1970-01-01
    • 2023-03-11
    • 2015-05-05
    • 2023-03-09
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    相关资源
    最近更新 更多