android笔记--解决SkImageDecoder::Factory returned null

使用HttpClient方式从网络上下载一张大的图片的Bitmap对象,用ImageView显示出来,运行下面的代码会下面的错误:

05-10 13:42:57.206: D/skia(2250): --- SkImageDecoder::Factory returned null

 代码:

package com.test.demo;

import java.io.InputStream;
import java.sql.Date;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.util.Log;

public class ImageUtil {

	public static Bitmap decodeSampledBitmapFromResource(Resources res,
			int resId, int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}

	public static Bitmap decodeSampledBitmapFromStream(InputStream is,
			int reqWidth, int reqHeight, Rect outPadding){
		
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(is, outPadding, options);
		
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		Log.i("outPadding", "---------");
		return BitmapFactory.decodeStream(is, outPadding, options);
	}
	
	public static Bitmap decodeSampledBitmapFromByteArray(byte[]data,
			int reqWidth, int reqHeight, Rect outPadding){
		
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeByteArray(data, 0, data.length, options);
		
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		Log.i("outPadding", "---------");
		return BitmapFactory.decodeByteArray(data, 0, data.length, options);
	}
	
	
	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

			final int halfHeight = height / 2;
			final int halfWidth = width / 2;

			// Calculate the largest inSampleSize value that is a power of 2 and
			// keeps both
			// height and width larger than the requested height and width.
			while ((halfHeight / inSampleSize) > reqHeight
					&& (halfWidth / inSampleSize) > reqWidth) {
				inSampleSize *= 2;
			}
		}

		return inSampleSize;
	}
}

 

package com.test.demo;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class LoadbigimageActivity extends Activity {

	private ImageView imageView;
	private DownloadTask task;
	private ImageUtil util;
	private String url = "https://unsplash.imgix.net/photo-1430132594682-16e1185b17c5?fit=crop&fm=jpg&h=700&q=75&w=1050";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loadbigimage);
		imageView = (ImageView) findViewById(R.id.imageView1);
		util = new ImageUtil();

		task = new DownloadTask();
		Matrix matrix = imageView.getImageMatrix();
		Log.i("outPadding", matrix.toString());
		task.execute(url);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.loadbigimage, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class DownloadTask extends AsyncTask<String, Void, Bitmap> {

		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			Bitmap bitmap = null;
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet get = new HttpGet(params[0]);
			try {
				HttpResponse response = httpClient.execute(get);
				if (response.getStatusLine().getStatusCode() == 200) {

					BufferedHttpEntity bufEntity = new BufferedHttpEntity(
							response.getEntity());

					InputStream inputStream = bufEntity.getContent();
					Rect outPadding = new Rect();
					byte[] data = EntityUtils.toByteArray(bufEntity);
					
					
					bitmap = ImageUtil.decodeSampledBitmapFromStream(
							inputStream, 100, 100, outPadding);
					
					
//					bitmap = ImageUtil.decodeSampledBitmapFromByteArray(data,
//							100, 100, outPadding);



					Log.i("outPadding", outPadding.toString());
				} else {
					Log.i("outPadding", "network error");
				}

			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			imageView.setImageBitmap(result);

		}

	}
}

 

解决方法一:不使用decodeStream,而是使用decodeByteArray方式进行获取Bitmap.

bitmap = ImageUtil.decodeSampledBitmapFromByteArray(data,100, 100, outPadding);

 

解决方法二:

stackverflow相关讨论:

http://stackoverflow.com/questions/12006785/android-skimagedecoder-factory-returned-null

按照下面的思路

I have encountered the same problem. And also I make sure the url is correct to download the image. By debugging the code, I found the variable of position in inputstream was set to 1024 after the first decode. So I add inputstream.reset() before the second decode. That works. Hope can help others.

修改decodeSampledBitmapFromStream函数,在第一次decodeStream后面调用InputStream的reset(),问题解决。

修正后代码如下

	public static Bitmap decodeSampledBitmapFromStream(InputStream is,
			int reqWidth, int reqHeight, Rect outPadding){
		
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(is, outPadding, options);
		try {
			is.reset();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);
		options.inJustDecodeBounds = false;
		Log.i("outPadding", "---------");
		return BitmapFactory.decodeStream(is, outPadding, options);
	}

 

转载于:https://www.cnblogs.com/xiaozu/p/4492190.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"cannot find -lphread collect2: error: ld returned 1 exit status" 这个错误通常是在编译过程中缺少了一个名为libpthread.so的库。这个库是用来支持多线程的,所以缺少它会导致编译错误。解决这个问题的方法是确保系统中已经安装了libpthread库,并且在编译时将其正确地链接到项目中。具体的解决方法可能因为不同的操作系统和编译环境而有所不同,你可以参考以下几种可能的解决方法: 1. 确认是否已经安装了libpthread库。你可以使用命令`ldconfig -p | grep libpthread`来查看系统中是否存在这个库。如果没有安装,你可以使用包管理器来安装它。例如,在Ubuntu上,你可以使用apt-get命令执行`sudo apt-get install libpthread-stubs0-dev`来安装。 2. 如果已经安装了libpthread库,但是仍然出现这个错误,可能是因为编译器无法找到这个库。你可以尝试通过设置环境变量来解决这个问题。例如,在Linux上,你可以使用export命令将库所在的路径添加到LD_LIBRARY_PATH环境变量中,然后重新编译项目。 3. 另一种可能的解决方法是检查你的编译选项是否正确。如果你使用的是gcc编译器,你可以尝试在编译命令中添加`-lpthread`选项,这样编译器就会将libpthread库链接到项目中。例如,你可以执行`gcc -o myprogram myprogram.c -lpthread`来编译一个名为myprogram的程序,其中myprogram.c是你的源代码文件。 总之,"cannot find -lphread collect2: error: ld returned 1 exit status"错误通常是由于缺少libpthread库或者编译选项不正确导致的。你可以尝试安装缺少的库,设置正确的环境变量或者调整编译选项来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Qt报错:cannot find -lws_32 collect2: error: ld returned 1 exit status](https://blog.csdn.net/weixin_37653181/article/details/127255099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [/usr/bin/ld: cannot find -lpcap问题的解决及广义化解决方法](https://blog.csdn.net/phmatthaus/article/details/124501460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [qt环境安装](https://download.csdn.net/download/hanqian3956/5943951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值