Android 如何将图像文件从URL下载到ByteArray?
以下是我的代码:Android 如何将图像文件从URL下载到ByteArray?,android,image,download,Android,Image,Download,以下是我的代码: private byte[] downloadImage(String image_url) { byte[] image_blob = null; URL _image_url = null; HttpURLConnection conn = null; InputStream inputStream = null; try {
private byte[] downloadImage(String image_url) {
byte[] image_blob = null;
URL _image_url = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
try {
_image_url = new URL(image_url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) _image_url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
conn.setDoInput(true);
try {
conn.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn.setUseCaches(false);
try {
inputStream = conn.getInputStream();
inputStream.read(image_blob);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn.disconnect();
}
return image_blob;
}
我要做的是获取图像的字节数组。在包裹中使用它将其转移到其他活动
使用此代码将报告NullPointerException。有人能说什么不对吗?您可能想这样试试:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(imageUrl);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
int imageLength = (int)(entity.getContentLength());
InputStream is = entity.getContent();
byte[] imageBlob = new byte[imageLength];
int bytesRead = 0;
while (bytesRead < imageLength) {
int n = is.read(imageBlob, bytesRead, imageLength - bytesRead);
if (n <= 0)
; // do some error handling
bytesRead += n;
}
DefaultHttpClient=newdefaulthttpclient();
HttpGet请求=新的HttpGet(imageUrl);
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
int imageLength=(int)(entity.getContentLength());
InputStream=entity.getContent();
byte[]imageBlob=新字节[imageLength];
int字节读取=0;
while(字节读取<图像长度){
int n=is.read(imageBlob、bytesRead、imageLength-bytesRead);
如果(n而不是发送图像,则可以发送缓存中下载的图像的路径。您可以使用此方法提供图像路径并将图像下载到本地路径
private String createLocal(String surl) {
URL url;
try {
url = new URL(surl);
String tempname=String.valueOf(surl.hashCode());
File root=getCacheDir();
File localfile=new File(root.getAbsolutePath()+"/"+tempname);
localfile.deleteOnExit();
if(!localfile.exists()){
InputStream is=url.openStream();
OutputStream os = new FileOutputStream(localfile);
CopyStream(is, os);
os.close();
}
return localfile.getAbsolutePath();
} catch (Exception e){
return null;
}
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size=1024;
try {
byte[] bytes = new byte[buffer_size];
for(;;) {
int count=is.read(bytes, 0, buffer_size);
if(count == -1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
您的byte[]image\u blob为空,在使用它之前,必须有足够的新空间:
image_blob = new byte[enough];
inputStream.read(image_blob);
您介意告诉我们NPE发生在哪一行吗?请将完整的stacktrace添加到您的问题中?这似乎适用于某些文件,但其他一些文件不以这种方式下载。由于某些原因,似乎无法始终提前知道内容的长度…无法导入DefaultHttpClient类。我是否必须添加依赖项还是库?
public static byte[] getByteArray(String url) throws IOException {
InputStream inputStream = (InputStream) new URL(url).getContent();
return IOUtils.toByteArray(inputStream);
}