2

我的应用程序中的一个功能是通过相机应用程序拍照,将其保存在外部存储中,然后将其放置在调用相机的活动中的 ImageView 中。代码如下所示:

public class ProjectActivity extends Activity {

private static final String JPEG_FILE_SUFFIX = ".jpeg";
private Bitmap _ImageBitmap;
private ImageView _ImageView;
private int _index;
private String _CurrentPhotoPath;

onCreate 方法找到 ImageView 并在位图成员已启动时对其进行设置(对于我以后要添加的功能):

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

    if(_ImageBitmap != null)
        _ImageView.setImageBitmap(_ImageBitmap);
}

如果位图已经初始化,onResume 方法应该将图像加载到 ImageView:

@Override
protected void onResume() {
    if(_ImageBitmap != null)
        _ImageView.setImageBitmap(_ImageBitmap);
    super.onResume();
}

拍照的方法,它们都可以正常工作,直到恢复活动:

public void takePicture(View view){
    this.dispatchTakePictureIntent(0);
}

/**
 * invokes an intent to take a picture
 * @param actionCode
 */
private void dispatchTakePictureIntent(int actionCode) {
    String action = MediaStore.ACTION_IMAGE_CAPTURE;
    if(Data.isIntentAvailable(this, action)){
            if(this.isExternalStorageWritable()){
            File f = createImageFile();
            if(f != null){
                Intent takePictureIntent = new Intent(action);

                //takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(takePictureIntent, actionCode);
            }
        }
    }
}

/**
 *  Checks if external storage is available for read and write 
 */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

private File createImageFile() {
    // Create an image file name
    String imageFileName = this._projName + getIndex();
    File image;
    File dir = getAlbumDir(this, this.getAlbumName());
    try {
        image = File.createTempFile(
            imageFileName, 
            JPEG_FILE_SUFFIX, 
            dir
        );
        _CurrentPhotoPath = image.getAbsolutePath();
        return image;
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        return null;
    }
}

private File getAlbumDir(Context context, String albumName) {
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    file.mkdirs();
    if (!file.isDirectory()) {
        return null;
    }
    return file;
}

private String getAlbumName() {
    return this._projName;
}

private String getIndex() {
    this._index++;
    return String.valueOf(this._index);
}

从图像捕获操作返回后,onActivityResult 返回 resultCode -1 和来自相机应用程序的数据:

protected void onActivityResult (int requestCode, int resultCode, Intent data){
    switch(requestCode){
        case 0:
            if(resultCode != Activity.RESULT_CANCELED){
                handleSmallCameraPhoto(data);
            }
            break;
        default: break;
    }
}

在这种方法中,我从意图中提取位图并将其加载到 ImageView

/**
 * adds the picture unto an ImageView
 * @param intent
 */
private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    _ImageBitmap = (Bitmap) extras.get("data");
    _ImageView.setImageBitmap(_ImageBitmap);
}

}

这是布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProjectActivity" >

<ImageView
    android:id="@+id/project_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/project_image_view" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/add_picture_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_picture_button_text"
        android:onClick="takePicture" />

    <Button
        android:id="@+id/make_movie_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/make_movie_button_text" />

    <Button
        android:id="@+id/edit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/edit_button_text" />

</LinearLayout>

我有相机和外部存储权限。

当我对此进行测试时,我可以在拍摄并返回此活动后看到图片,但是一旦调用 onResume (由于相机应用程序的监视器旋转),图像就会消失,我只能看到按钮。

帮助将不胜感激,伊丹

更新:由于某种原因,第二次调用 onResume 时 _ImageBitmap 为空。任何想法为什么会发生这种情况?

4

0 回答 0