我正在尝试从手机上的特定文件路径设置图片。该文件路径是指手机上的照片。文件路径可能看起来像这样:
/storage/emulated/0/Pictures/picture.jpg
以下是代码。
Bitmap image = null;
//trying to set the image from filepath
try {
image = BitmapFactory.decodeStream((InputStream) new URL(filepath).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (image == null) {
//This becomes true because the image is not set
Toast.makeText(getApplicationContext(),"image == null",Toast.LENGTH_LONG).show();
}
最终,图像未设置。
URL
还是URI
? - EmilAsyncTask
从URL下载图像。 - Rustamimgurl
吗?它实际上是一个文件路径,不是吗? - EmilBitmap bitmap = BitmapFactory.decodeFile(filePath)
。 - Emil