想把本地的图片放到imageview里面。但运行出错,请问如何解决?

package puzzle.test;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class PuzzleActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);

button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

//取得返回数据的Uri
Uri imageFileUri = data.getData();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bmp = null;
try
{
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageView imv = (ImageView) findViewById(R.id.imageView1);
imv.setImageBitmap(bmp);
}
}
}
想把本地的图片放到imageview里面。但运行出错。什么都没有。求解答啊求解答T^T 

精慕HU
浏览 219回答 2
2回答

qq_遁去的一_1

bmpFactoryOptions的用法错了,设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度。有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。下面添加了一种动态计算的方法,使用该算法,就可动态计算出图片的inSampleSize。protected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {// 取得返回数据的UriUri imageFileUri = data.getData();BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();bmpFactoryOptions.inJustDecodeBounds = true;try {BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}bmpFactoryOptions.inSampleSize = computeSampleSize(bmpFactoryOptions, -1, 800 * 480);bmpFactoryOptions.inJustDecodeBounds = false;Bitmap bmp = null;try {bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}ImageView imv = (ImageView) findViewById(R.id.imageView1);imv.setImageBitmap(bmp);}}public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);int roundedSize;if (initialSize <= 8) {roundedSize = 1;while (roundedSize < initialSize) {roundedSize <<= 1;}} else {roundedSize = (initialSize + 7) / 8 * 8;}return roundedSize;}private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {double w = options.outWidth;double h = options.outHeight;int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength),Math.floor(h / minSideLength));if (upperBound < lowerBound) {// return the larger one when there is no overlapping zone.return lowerBound;}if ((maxNumOfPixels == -1) && (minSideLength == -1)) {return 1;} else if (minSideLength == -1) {return lowerBound;} else {return upperBound;}}&nbsp;

慕田峪4524236

个人猜测你的代码应该抛出了空指针异常bmpFactoryOptions.inJustDecodeBounds = true;这句话去掉就没问题了,但是以后可能还会抛OOM异常bmpFactoryOptions的用法好像你还不会,那就先不要乱用
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android