我有一个URI像它具有图像如何从Android中的URL获取位图?
file:///mnt/...............
如何使用URI来获得图像,但它返回null,请告诉我,我错了。
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
我有一个URI像它具有图像如何从Android中的URL获取位图?
file:///mnt/...............
如何使用URI来获得图像,但它返回null,请告诉我,我错了。
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath());
Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
这是一个简单的一条线的方式来做到这一点:所以你想从一个文件中的位图
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
这应该做的伎俩:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} // Author: silentnuke
不要忘记添加Internet权限在您的清单。
怎么回合我的网址有https连接? – 2017-04-10 07:39:04
好吗?标题说URL。无论如何,当你从Android的外部存储获取文件时,你绝对不应该使用直接路径。相反,调用getExternalStorageDirectory()像这样:
File bitmapFile = new File(Environment.getExternalStorageDirectory() + "/" + PATH_TO_IMAGE);
Bitmap bitmap = BitmapFactory.decodeFile(bitmapFile);
getExternalStorageDirectory()为您提供了路径的SD卡。 您还需要在Manifest中声明WRITE_EXTERNAL_STORAGE权限。
这不是一句话,因为创建你的url对象应该被try-catch包围。 – portfoliobuilder 2015-03-17 22:14:09
这个想法是获得位图的一种方式,因为您已经定义了一个url。 – brthornbury 2015-04-19 04:36:38
'url.openConnection()。getInputStream()'可以被替换为简单的'url.openStream()' – 2015-12-15 21:14:03