我正在使用我的localhost来获取图像并在ImageView中查看。由于某种原因,我得到Factory返回null错误。我已多次查看代码,但我没有看到错误。任何帮助将不胜感激!
GalleryZoom.java
public class Zoom extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.gallery_zoom);
String selection = getIntent().getExtras().getString("image");
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
new backgroundLoader().execute();
}
private class backgroundLoader extends AsyncTask<Void, Void, Void> {
Bitmap bmp;
@Override
protected Void doInBackground(Void... params) {
bmp = DecodeBitmapSampleSize(getIntent().getExtras().getString("image"), 48, 64);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ImageView image = (ImageView) findViewById(R.id.imageZoom);
image.setImageBitmap(bmp);
}
}
public Bitmap DecodeBitmapSampleSize (String strURL, int reqWidth, int reqHeight) {
InputStream in = null;
Bitmap bmp = null;
in = OpenHttpConnection(strURL);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
options.inSampleSize = calculateSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(in, null, options);
return bmp;
}
private InputStream OpenHttpConnection(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(connection.getInputStream());
return in;
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
public static int calculateSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int width = options.outWidth;
final int height = options.outHeight;
int inSampleSize = 1;
if (width > reqWidth || height > reqHeight) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
}
LogCat日志
08-13 21:55:19.578: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:19.658: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.688: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:20.628: I/MemoryCache(3197): cache size = 71600, length = 2
08-13 21:55:20.678: I/MemoryCache(3197): cache size = 101408, length = 3
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:26.998: D/skia(3197): --- SkImageDecoder::Factory returned null
答案 0 :(得分:62)
我遇到了同样的问题。此外,我确保下载图像的URL正确。通过调试代码,我发现在第一次解码后输入流中的位置变量被设置为1024。 所以我在第二次解码之前添加 inputstream.reset()。这样可行。 希望可以帮助别人。
答案 1 :(得分:12)
嗨,伙计们,我环顾四周后得到了最好的解决方案。 正如#jia George指出你在第一次解码后重置输入流,问题是不支持某些时间重置,但你可以将输入流包装在BufferedInputStream中,这个很好。
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BufferedInputStream buffer=new BufferedInputStream(is);
BitmapFactory.decodeStream(buffer,null,options);
buffer.reset();
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
BitmapFactory.decodeStream(buffer,null,options);
答案 2 :(得分:12)
这可能是一种罕见的情况,但对于那些使用Picasso的人,如果您尝试从URL加载图片但URL未引用图像,则会看到此错误。
例如:
http://www.google.ca
而不是:
https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
答案 3 :(得分:1)
在从图库应用程序返回的Intent中读取流时,我遇到了类似的问题。 inputstream.reset()抛出一个IOException,正如Shellum上面提到的那样,所以我通过关闭流并再次重新打开来解决它。很简单,并且做到了。
答案 4 :(得分:1)