亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想把本地的圖片放到imageview里面。但運行出錯,請問如何解決?

想把本地的圖片放到imageview里面。但運行出錯,請問如何解決?

精慕HU 2022-04-21 16:11:05
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. */@Overridepublic 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 stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {//取得返回數據的UriUri 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 blocke.printStackTrace();}ImageView imv = (ImageView) findViewById(R.id.imageView1);imv.setImageBitmap(bmp);}}}想把本地的圖片放到imageview里面。但運行出錯。什么都沒有。求解答啊求解答T^T 
查看完整描述

2 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

bmpFactoryOptions的用法錯了,設置inJustDecodeBounds為true后,decodeFile并不分配空間,但可計算出原始圖片的長度和寬度。有了這兩個參數,再通過一定的算法,即可得到一個恰當的inSampleSize。下面添加了一種動態計算的方法,使用該算法,就可動態計算出圖片的inSampleSize。

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;

try {
BitmapFactory
.decodeStream(
getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.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 block
e.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;
}

}

 


查看完整回答
反對 回復 2022-04-24
?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

個人猜測你的代碼應該拋出了空指針異常
bmpFactoryOptions.inJustDecodeBounds = true;
這句話去掉就沒問題了,但是以后可能還會拋OOM異常
bmpFactoryOptions的用法好像你還不會,那就先不要亂用

查看完整回答
反對 回復 2022-04-24
  • 2 回答
  • 0 關注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號