我想设置我的imageButtons图像,当用户从图库中选择一张照片或正在使用相机拍照时。问题是我的imageButtons只是变成了一个空白图像,但我正在获取文件目录。我究竟做错了什么?我从这个答案camera & gallery创建了我的ImageIntent和onActivityResult。但这里是我的onActivityResult方法:setImageURI无法正常工作
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
selectedImageUri = data == null ? null : data.getData();
}
ImageButton pic1 = (ImageButton) findViewById(R.id.ibPic1);
Toast.makeText(this, "Image saved to:\n" + selectedImageUri,
Toast.LENGTH_LONG).show();
pic1.setImageURI(selectedImageUri);
}
}
}
所以我从我收到的URI的吐司知道。我试过this answer以及其他各种涉及某种Bitmap的解决方案,但这些解决方案总是导致应用程序崩溃和内存不足异常。
编辑
OnClick方法来启动图像意图:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ibPic1:
openImageIntent();
break;
}
}
图像意图方法
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Klea" + File.separator);
root.mkdirs();
final String fname = Sell.getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Vælg kilde");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
获取唯一的文件名的方法:
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
画廊和相机都不工作?你能检查两者吗? – berserk 2014-10-28 18:43:43
两个都没有工作。敬酒说相机的“file:// .....”和相册的“content:// ......”。 – 2014-10-28 18:45:37
在相机中,你会得到确切的uri,但是在图库的情况下,你会得到内容Uri。 – berserk 2014-10-28 18:46:28