我從一個活動(通過startActivityForResult)檢索一個Uri,我想在對話框中顯示它,但我無法得到它的工作,我得到一個NullPointerException的ImageView時,我嘗試設置它的URI。Android AlertDialog與圖像裏面
這裏就是我試圖做的:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
final Uri imageUri = imageReturnedIntent.getData();
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.image_dialog, null);
adb
.setTitle("Confirm picture?")
.setView(view)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
uploadDocument(imageUri);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
AlertDialog dialog = adb.create();
// This fails:
((ImageView) dialog.findViewById(R.id.dialog_imageview)).setImageURI(imageUri);
dialog.show();
}
這裏的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/dialog_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我錯過了什麼?
編輯:
我也試過直接從意圖獲取位圖,因爲在Android DOC(https://developer.android.com/training/camera/photobasics.html)指出:
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
但我得到的samre結果(空/空格) 。
試過了已經,這是行不通的,我只是有一個空白(白色)的空間,而不是圖片:https://t58i.imgup.net/Screenshot3b5f.png –