来自Android URL的位图
我正在尝试从URL获取png位图,但该位图在以下代码中始终为空:来自Android URL的位图,android,url,bitmap,Android,Url,Bitmap,我正在尝试从URL获取png位图,但该位图在以下代码中始终为空: private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; Activity activity; public DownloadImageTask(ImageView bmImage, Activity activity) { this.bmIm
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
Activity activity;
public DownloadImageTask(ImageView bmImage, Activity activity) {
this.bmImage = bmImage;
this.activity = activity;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Log.i("LEGGERE", urldisplay);
Bitmap mIcon11 = null;
try {
URL url = new URL(urldisplay);
mIcon11 = BitmapFactory.decodeStream(url.openConnection().getInputStream());
if (null != mIcon11)
Log.i("BITMAP", "ISONOTNULL");
else
Log.i("BITMAP", "ISNULL");
} catch (Exception e) {
Log.e("Error", "PORCA VACCA");
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
我会犯一些错误吗?这是一个简单的单行方法:
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
这是一种简单的单行方式:
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
如果在浏览器中尝试http url,您会看到它重定向到https。那是你的问题。BitmapFactory.decodeStream不会执行此重定向,因此它返回null。如果在浏览器中尝试http url,则会看到它重定向到https。那是你的问题。BitmapFactory.decodeStream不会执行此重定向,因此它返回null。您可以尝试下面的代码
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
int IO_BUFFER_SIZE = 4 * 1024;
try {
URI uri = new URI(url);
url = uri.toASCIIString();
in = new BufferedInputStream(new URL(url).openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
int bytesRead;
byte[] buffer = new byte[IO_BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
} catch (IOException e) {
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
您可以尝试下面的代码
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
int IO_BUFFER_SIZE = 4 * 1024;
try {
URI uri = new URI(url);
url = uri.toASCIIString();
in = new BufferedInputStream(new URL(url).openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
int bytesRead;
byte[] buffer = new byte[IO_BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
} catch (IOException e) {
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
要从URL获取位图,请尝试以下操作:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.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;
}
}
或:
要从URL获取位图,请尝试以下操作:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.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;
}
}
或:
我建议使用THAD(异步)来避免异常错误:
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
Bitmap thumb;
// Ur URL
String link = value.toString();
try {
URL url = new URL(link);
thumb = BitmapFactory.decodeStream(url.openConnection().getInputStream());
// UI component
imageView.setImageBitmap(thumb);
} catch (Exception e) {
Log.e("error message", Objects.requireNonNull(e.getMessage()));
}
}
});
thread.start();
我建议使用THAD(异步)来避免异常错误:
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
Bitmap thumb;
// Ur URL
String link = value.toString();
try {
URL url = new URL(link);
thumb = BitmapFactory.decodeStream(url.openConnection().getInputStream());
// UI component
imageView.setImageBitmap(thumb);
} catch (Exception e) {
Log.e("error message", Objects.requireNonNull(e.getMessage()));
}
}
});
thread.start();
考虑使用毕加索或GlideCheck的答案:你有没有在网上下载许可证你下载什么?一个.jpg文件?它的尺寸是多少?首先尝试保存下载的文件。如果.jpg太大,由于内存不足,无法制作位图。@PhanVănLinh是的,我有互联网许可证可以考虑使用毕加索或Glide查看以下答案:在清单中你有互联网许可证吗?你下载的是什么?一个.jpg文件?它的尺寸是多少?首先尝试保存下载的文件。如果.jpg太大,由于内存不足,无法制作位图。@PhanVănLinh是的,我有互联网许可证是的。但是你为什么要把它作为一个答案呢?OP已经在做这个了,是的。但是你为什么要把它作为一个答案呢?OP已经在做这件事了。