【问题标题】:Android Upload Image using path or uri?Android使用路径或uri上传图片?
【发布时间】:2019-02-19 10:03:06
【问题描述】:

我无法将图片从图库中导入到我的应用中。

困难在于,在某些文章中他们使用 uri 而不是路径,但在其他文章中反之亦然。

另外,我也不确定......当我从意图返回的 Uri 中获取 Uri 时,我应该使用光标来获取图像数据吗? (How to get Images from Cursor in android?) 在其他一些参考资料中,他们以简单的方式做到这一点,只需使用 'getPath()' 方法。

我需要 path 或 uri 吗?还是只有其中之一?

我现在很困惑..

【问题讨论】:

  • Uri.fromFile(new File("/sdcard/sample.jpg"))
  • @RamMohandubey 在某些示例中,他们将 uri 转换为路径,在其他示例中将路径转换为 ​​uri。什么是正确的?
  • 试试这个:journaldev.com/23219/…

标签: android uri android-file


【解决方案1】:

始终使用 Uri:

File file = new File(uri.getPath());

然后

Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());

当您有位图时,只需将其加载到 ImageView 中

imageView.setImageBitmap(bitmap);

【讨论】:

  • 同时检查文件是否存在
  • 谢谢!如果 uri 是真正需要的,那么,如果我可以直接获取 uri,使用“data,getdata()”(我使用了“ACTION_PICK”意图),我不需要获取此文件的路径吗?
  • 你能贴一些代码,这样会更清楚吗?
猜你喜欢
  • 2011-10-19
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2012-04-30
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多