本文介绍了Android从文件路径获取图像到位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从电话上的特定文件路径设置图像.文件路径是手机上的照片.文件路径可能如下所示
/storage/emulated/0/Pictures/picture.jpg
这是代码.
I'm trying to set the image from a specific filepath ON THE PHONE. The filepath is to the photo on the phone. the filepath could look like this
/storage/emulated/0/Pictures/picture.jpg
Here is the code.
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();
}
最后没有设置图像.
推荐答案
使用此方法从文件路径中获取位图
Use This Method to get Bitmap from Filepath
public Bitmap getBitmap(String path) {
Bitmap bitmap=null;
try {
File f= new File(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap ;
}
这篇关于Android从文件路径获取图像到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!