0
我正在創建一個應用程序,在點擊動作欄項目時,照相機活動開始,並且在拍攝並保存照片後,將其顯示在主活動。在肖像模式下拍攝的圖片應該保留在橫向模式下android
現在,如果我在縱向方向上從MainActivity啓動相機,並在相機活動中切換到橫向,拍攝照片並單擊保存按鈕,它將返回到MainActivity,但圖像不會顯示,這是我猜,因爲主要活動正在重新加載,但我不確定。
即使改變方向後,如何保留圖像。
MainActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// file creation for saving video
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (fileUri != null) {
targetFile = fileUri.getPath();
// setting file image name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
return true;
onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture);
if (targetFile != null) {
theFile = targetFile;
imgPicture.setImageBitmap(BitmapFactory.decodeFile(theFile));
}
} else if (resultCode == RESULT_CANCELED) {
} else {
Toast.makeText(this, "Your picture could not be saved.", Toast.LENGTH_LONG).show();
}
}
}
的Manifest.xml
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ActionBarTest.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
做你有onActivityResult()方法? – JRowan 2013-03-05 21:39:56
我已經更新了問題並添加了onActivityResult()方法。謝謝 ! – user2112791 2013-03-05 21:48:43
也是實際圖像的大小吃了很多內存,你可能會考慮縮小它 – JRowan 2013-03-05 21:57:25