使用相机的Android应用程序,屏幕旋转后照片消失

时间:2014-06-22 15:45:48

标签: android camera rotation photo

该应用程序的目的是拍摄照片并在之后显示在屏幕上。我已将屏幕旋转锁定为纵向,但在照片被拍摄并连接到应用程序屏幕后,手机的屏幕可以再次旋转,如果发生这种情况,图像就会消失。我该如何解决这个问题?这是代码

public class MainActivity extends Activity {

private static String logtag = "CameraApp";
private static int TAKE_PICTURE = 1;
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button cameraButton = (Button)findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);
}

private OnClickListener cameraListener = new OnClickListener() {
    public void onClick(View v) {
        takePhoto(v);
    }
};

public void takePhoto(View v) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
    case 1:
        if(resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView)findViewById(R.id.image_view_camera);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
                imageView.setImageBitmap(bitmap);
                Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
            } catch(Exception e) {
                Toast.makeText(MainActivity.this, "failed to load", Toast.LENGTH_LONG).show();
                Log.e(logtag, e.toString());
            }
        }
    }
  }
}

3 个答案:

答案 0 :(得分:3)

当更改屏幕方向时,系统将重新启动活动(调用onDestroy()然后调用onCreate())。所以我相信你需要在重启后再次设置图像。

此链接http://developer.android.com/guide/topics/resources/runtime-changes.html提供了有关如何处理此问题的一些有用信息。最简单的方法可能是将图像uri存储在onSaveInstanceState()中,然后在onRestoreInstanceState()中恢复图像。

答案 1 :(得分:0)

为每个视图设置ID,以防止在屏幕更改方向时丢失数据

  1. 在值中创建一个xml文件并将其命名为ids.xml
  2. 像这样创建你的ID

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item name="button_1_id" type="id"/>
        <item name="button_2_id" type="id"/>
        <item name="imageView_id" type="id"/>
        ....
    </resources>
    
  3. 设置ID:

    button_1.setId(R.id.button_1_id);
    ...
    

答案 2 :(得分:-3)

它是一个快速解决方案..它确实解决了问题,但你可以通过将此代码添加到MainActivity所在的清单中来阻止屏幕更改方向..只需添加此行..

<activity
        ...
        android:screenOrientation="portrait" 
        ...>
</activity>