【问题标题】:No photo in ImageView after return from Intent, handling life-cycle eventsIntent返回后ImageView中没有照片,处理生命周期事件
【发布时间】:2013-02-19 20:14:50
【问题描述】:

我想构建一个可以存储文本和图像的简单应用程序。我从第一个和第二个练习的记事本教程 (http://developer.android.com/training/notepad/index.html) 开始。 在我做了第二次之后,我安排了处理图像的练习,除非用户点击设备中的后退按钮,否则它工作得很好。在帮助下进行了以处理生命周期事件为目标的练习。我设法做到了,它对于数据库中的旧条目非常有效,但是当我尝试添加新图像时,我的 ImageView 没有填充刚刚拍摄的照片。 你能发现问题出在哪里吗?

public class ParagonArmageddonEdit extends Activity
{

private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private ImageView imageContainer;
private byte[] imageInByteArray;
private Button takePhotoButton;
private ParagonDbAdapter mDbHelper;

private static final long serialVersionUID = 1L;
public boolean classEnabled;
private static final int CAMERA_REQUEST = 1888;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    mDbHelper = new ParagonDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.paragonarmageddon_edit);
    setTitle(R.string.edit_note);

    this.imageContainer = (ImageView) this.findViewById(R.id.ImageView);
    this.takePhotoButton = (Button) this.findViewById(R.id.photoButton);
    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);

    Button confirmButton = (Button) findViewById(R.id.Save);

    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(ParagonDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(ParagonDbAdapter.KEY_ROWID)
                                : null;
    }
    populateFields();

    this.takePhotoButton.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            //camera Intent
            Intent IntentKamery = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(IntentKamery, CAMERA_REQUEST); 
        }
    });

    confirmButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });
}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mTitleText.setText(note.getString(
                note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_TITLE)));
        mBodyText.setText(note.getString(
                note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_BODY)));
        byte[] photo = note.getBlob(note.getColumnIndexOrThrow(ParagonDbAdapter.KEY_ZDJECIE));
        Bitmap photoConvertedToBitmap = BitmapFactory.decodeByteArray(zdjecie, 0, photo.length);
        this.imageContainer.setImageBitmap(photoConvertedToBitmap);
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
    {   
        Bitmap photoConvertedToBitmap = (Bitmap) data.getExtras().get("data"); 
        imageContainer.setImageBitmap(photoConvertedToBitmap);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photoConvertedToBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        imageInByteArray = stream.toByteArray();
        photoTaken = true;
    }  
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(ParagonDbAdapter.KEY_ROWID, mRowId);
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String title = mTitleText.getText().toString();
    String body = mBodyText.getText().toString();

    imageContainer.buildDrawingCache();
    Bitmap photoConvertedToBitmap = imageContainer.getDrawingCache();

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photoConvertedToBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    imageInByteArray = stream.toByteArray();

    if (mRowId == null) {
        long id = mDbHelper.createNote(title, body, imageInByteArray);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, title, body, imageInByteArray);
    }
}
}

【问题讨论】:

    标签: android sql android-camera android-imageview lifecycle


    【解决方案1】:

    Intent android.provider.MediaStore.ACTION_IMAGE_CAPTURE 不会在其“数据”Extra 中返回字节数组。它向图像返回一个Uri,该图像必须打开,例如

    Uri uri = data.getData();

    这提供了图像的内容路径,可以使用ContentResolver 加载:

    InputStream input = context.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input);
    

    您需要确保检查 Uri 是否存在并优雅地处理错误,您永远不应假设另一个应用程序会做一些明智的事情,并且应该尽可能预期格式错误/丢失的数据。

    【讨论】:

    • emmm...你一定是对的,但在我的例子中,用相机中的照片填充 ImageView 的代码工作得很好,直到我实现了处理生命周期的代码。
    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多