我想拍攝一張圖片,將filpath存儲在全局變量中,然後顯示縮略圖。但在保存圖像後,它無法顯示圖像。Android相機,保存圖像,然後顯示圖像
失敗於:
startActivityForResult(takePicture, 0);
//
//Camera Application For new Photo
public void btnPhotoClicked(View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Create the file path
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureName = getPictureName();
File imageFile = new File(pictureDirectory, pictureName);
Uri pictureUri = Uri.fromFile(imageFile);
//set the filepath to a global variable
imgurl = pictureUri.toString();
//save the picture
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
//start the activity
startActivityForResult(takePicture, 0);//zero can be replaced with any action code
}
這裏是顯示功能,它從取相片分離並挑相片
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
//Set the thumbnail
mImageView.setImageURI(selectedImage);
//set a textfield to the filepath
tximgurl.setText(imgurl);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
mImageView.setImageURI(selectedImage);
tximgurl.setText(imgurl);
}
break;
}
}
它不會崩潰,但這會失敗。 if(requestCode == CAMERA_PHOTO && resultCode == RESULT_OK) –
你有沒有定義private static final int CAMERA_PHOTO = 111? – Rajesh
是的,我有。在活動的頂部 –