Android - 保存的图像文件在onResume之后不加载

时间:2012-12-07 17:07:39

标签: android android-imageview android-camera-intent

我的应用中的一项功能是通过相机应用拍摄照片,将其保存在外部存储设备中,然后将其放入调用相机的活动中的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);
}

从Image Capture Action返回后,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被调用(由于相机应用程序的显示器旋转),图像消失,我只能看到的按钮。

帮助将不胜感激,Idan

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

0 个答案:

没有答案