1
我试图从URL下载图像。图像类型是PNG,分辨率是400x400像素。无法解码图像文件中的位图
这里是下载代码片段。
Bitmap bitmap=null;
URL imageUrl = new URL(url);
conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream ins=conn.getInputStream();
os = new FileOutputStream(f);
Utilities.getUtilities().copyStream(ins, os);
os.flush();
Log.i(TAG_NAME, "file size : "+ f.length());
Log.i(TAG_NAME, "file exists in cache? " + f.exists());
bitmap = decodeFile(f);
return bitmap;
这是文件编写器。
public 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){
ex.printStackTrace();
}
}
而且解码方法
private Bitmap decodeFile(File f){
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final int REQUIRED_SIZE = 400; //for testing, it is set to b a constant
System.out.println("REQUIRED_SIZE >>> " + REQUIRED_SIZE);
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
//o2.inJustDecodeBounds = true;
o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
o2.inSampleSize=scale; //scale is set off since android:src automatically scales the image to fit the screen
try {
return BitmapFactory.decodeStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
我可以看到,文件中的设备存在。但是,解码流失败。我花了数小时在互联网上搜索;尝试几乎所有的东西,没有成功,几乎滚滚而来。
解码流导致以下错误。
SkImageDecoder::Factory returned null
你发现了什么在这里失去了?
编辑:
问题现在已经得到解决。服务器期待cookie的详细信息,我没有附上。花了将近一天,在灌木丛周围跳动:-)
感谢所有的宝贵意见!
任何错误或只是回报空值?如果错误,发布logcat信息。 – 2013-03-13 14:42:57
对不起!糟糕,编辑问题! – Renjith 2013-03-13 14:45:21
希望你已经尝试过提到的解决方案[这里](http://stackoverflow.com/questions/12006785/android-skimagedecoder-factory-returned-null) – PCoder 2013-03-13 14:50:50