1
Folks。以编程方式从相机文件夹获取图像
我在这里面临一个奇怪的问题。我有一个应用程序,您可以从按钮点击按钮中的图库或相机中选择一个图像。除了每当我从我的相机文件夹中选择任何图片时,应用程序刚刚崩溃,一切进展顺利。下面我的代码
case R.id.camera:
AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent.createChooser(intent,
"Choose a Picture");
startActivityForResult(chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String mImageCaptureUri = null;
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(cameraIntent,
ACTION_REQUEST_CAMERA);
break;
}
}
});
builder.show();
break;
活动的结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ACTION_REQUEST_GALLERY:
Uri selectedImageUri;
selectedImageUri = data == null ? null : data.getData();
cap.setImageURI(selectedImageUri);
break;
case ACTION_REQUEST_CAMERA:
Bitmap photo = (Bitmap) data.getExtras().get("data");
cap.setImageBitmap(photo);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
Toast.makeText(getApplicationContext(),
finalFile.getAbsolutePath(), 4000).show();
break;
}
}
}
请张贴您的logcat与错误的详细信息。 – NigelK
不幸的是我用我的手机来测试它,因为模拟器没有任何图片可供选择。 –
它只与相机文件夹照片崩溃,我的我的Facebook相册在我的画廊,它工作正常。 –