1
我遇到問題。我編寫了一個應用程序來選擇圖像並將其傳遞給另一個活動,並將其顯示在第二個活動的屏幕上。但是,我發現我的應用程序在收到高分辨率圖像後崩潰。我在我的測試中發送了一張1440x810圖片(大小爲166KB)。Android:setImageBitmap在我的應用程序從Intent接收到高分辨率圖像時失敗
下面是我的代碼:
第一個活動
public class MainActivity extends ActionBarActivity {
private Button button;
private Intent intent ;
public final static String EXTRA_BITMAP_BYTE_STREAM = "EXTRA_BITMAP_BYTE_STREAM";
private static final int PICK_IMAGE = 1;
private String imageFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
setActionListener();
Intent getImgIntent = new Intent();
getImgIntent .setType("image/*");
getImgIntent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(getImgIntent , "Select Picture"), PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the image
imageFilePath = cursor.getString(0);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void setActionListener(){
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap resImage;
ByteArrayOutputStream outImageByteArrayOutputStream = new ByteArrayOutputStream();
byte [] outImageByteArray;
intent = new Intent(MainActivity.this,SecMainActivity.class);
File file = new File(imageFilePath);
resImage = BitmapFactory.decodeFile(imageFilePath);
resImage.compress(Bitmap.CompressFormat.JPEG,100,outImageByteArrayOutputStream);
outImageByteArray = outImageByteArrayOutputStream.toByteArray();
intent.putExtra(EXTRA_BITMAP_BYTE_STREAM, outImageByteArray);
startActivity(intent);
}
});
}
}
次活動
public class SecMainActivity extends ActionBarActivity {
private Button returnButton;
private ImageView receivedImageView;
Intent intent;
Thread handleBitmapImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec_main);
findViews();
handleReceivedInfoToUI();
setActionListener();
}
@Override
protected void onStart(){
super.onStart();
handleBitmapImage.start();
}
private void findViews(){
returnButton = (Button) findViewById(R.id.returnButton);
receivedImageView = (ImageView) findViewById(R.id.receivedImageView);
}
private void handleReceivedInfoToUI(){
intent = getIntent();
handleBitmapImage = new Thread(new Runnable(){
@Override
public void run(){
byte [] receivedImageByteArray = intent.getByteArrayExtra(MainActivity.EXTRA_BITMAP_BYTE_STREAM);
final Bitmap bmp = BitmapFactory.decodeByteArray(receivedImageByteArray, 0, receivedImageByteArray.length);
runOnUiThread(new Runnable(){
@Override
public void run(){
setReceivedImage(bmp);
}
});
}
});
}
private void setActionListener(){
returnButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
finish();
}
});
}
public void setReceivedImage(Bitmap bmp){
receivedImageView.setImageBitmap(bmp);
}
}
我一定要壓縮和調整我接收到的圖像來解決這個問題?
我猜你有「outOfMemory錯誤」正確嗎?如果沒有,請發佈您的Logcat。 – JLONG 2014-12-05 03:52:11