我一直試圖在捕獲後將圖像數據傳回應用程序。但是,嘗試返回到我的應用程序時,它總是崩潰。無法將圖像數據傳遞迴Android應用程序
的代碼開始的意圖是:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView) findViewById(R.id.ImageView);
upload = (Button) findViewById(R.id.Upload);
snap = (Button) findViewById(R.id.Snap);
select = (Button) findViewById(R.id.Select);
subject = (EditText) findViewById(R.id.Subject);
msg = (EditText) findViewById(R.id.Message);snap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String imageFilePath = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(it, CAMERA_RESULT);
}
});
}
接收意向的代碼是:
case CAMERA_RESULT:
if (resultCode == Activity.RESULT_OK) {
// Get Extra from the intent
Bundle extras = data.getExtras();
// Get the returned image from extra
Bitmap bmp = (Bitmap) extras.get("data");
imgView = (ImageView) findViewById(R.id.ImageView);
imgView.setImageBitmap(bmp);
}
break;
我也收到以下例外情況發生應用程序崩潰時:
java.lang.RuntimeException:無法恢復活動{com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}: java.lang.RuntimeException:將結果ResultInfo {who = null,request = 100,result = -1,data = null}傳遞給activity {com.NYP.estatemanagement/com.NYP.estatemanagement.MainActivity}:java.lang。 NullPointerException異常
謝謝!它設法將圖像數據發送迴應用程序而不會崩潰。我可以再問兩個問題嗎?有沒有辦法讓圖像尺寸變大?第二個問題:有沒有一種方法可以持久存儲當方向發生變化或應用程序從暫停狀態返回時顯示圖像?再次感謝幫助= D – Benflop