我正在試圖拍照並在ImageView中顯示。如果我嘗試從圖庫中填充ImageView,它可以很好地工作,但是如果我使用相機拍攝照片,則不起作用。Android - 爲什麼ImageView是空的?
下面是我在做什麼:
//選擇採取從相機或畫廊
public class ProfileFragment extends Fragment
.....
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Escolha a Foto");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { // pick from
// camera
if (item == 0) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replced with any action code
} else {
// pick from file
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replced with any action code
}
}
});
....
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == -1) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
}
break;
case 1:
if (resultCode == -1) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
}
break;
}
}
private void captureImageInitialization() {
final String[] items = new String[] { "New Photo",
"Choose from Gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.select_dialog_item, items);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Photo");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replced with any action code
} else {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replced with any action code
}
}
});
dialog = builder.create();
}
在有什麼不對任何想法?
UPDATE:從片段的方法「onCreateView()」後,被稱爲保存照片......這就是爲什麼ImageView的是空的...我怎樣才能解決這個問題?
UPDATE2:從mainActivity的onCreate()被調用的一切,我用相機的時間。
在此先感謝。
你可以請顯示整個代碼,如適配器...? –
那個switch語句沒有任何意義。您可以在開關你的情況檢查的價值,並再次無論是'-1' – tolgap
ops..the值-1大約resultCode爲從「onActivityResult」和0 =採取從相機,1從畫廊 – placplacboom