gpt4 book ai didi

java - Android从文件路径获取图像到位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:07 25 4
gpt4 key购买 nike

我正在尝试从手机上的特定文件路径设置图像。文件路径是手机上的照片。文件路径可能如下所示

/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();
}

最终图像没有设置。

最佳答案

使用此方法从文件路径获取位图

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 ;
}

关于java - Android从文件路径获取图像到位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com