问题:我在三星 S3 的onActivityResult(int requestCode, int resultCode, Intent data)
中获取相机意图的数据为空。但是在其他一些设备上运行良好。我自定义了用于获取数据的代码,并在 Web 中搜索了此问题,但没有发现任何有用的内容。
法典:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_CAMERA && data != null && data.getData() != null)
else if (requestCode == TAKE_CAMERA) {
if (resultCode != RESULT_OK) return;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(tempFileUri, "image/*");
intent.putExtra("outputX", 90);
intent.putExtra("outputY", 90);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_CAMERA);
} else if (requestCode == CROP_CAMERA && data != null) {
Bitmap photo = data.getExtras().getParcelable("data");
try {
FileOutputStream out = new FileOutputStream(tempFileUri.getPath());
photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (photo != null) {
imagePhoto.setImageBitmap(photo);
<my code>
}
}
如果您提供MediaStore.EXTRA_OUTPUT,则意图为空,但您将在提供的文件中拥有照片(Uri.fromFile(f))。
我希望下面的链接为您提供解决方案。它对我有用。
http://mylearnandroid.blogspot.in/2014/02/multiple-choose-custom-gallery.html
我遇到了同样的问题,并得到了 解决方法 在下面的方式 它肯定会帮助你解决一次:
对于解决方案,我在下面引用了解决问题的链接:
三星银河S3相机解决方案
在调用捕获的图像时编写以下代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
并通过以下方式在OnActivityResult()方法中获取您的URI:
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
Log.i("TAG", "Inside PICK_FROM_CAMERA");
// Describe the columns you'd like to have returned. Selecting from
// the Thumbnails location gives you both the Thumbnail Image ID, as
// well as the original image ID
try {
Log.i("TAG", "inside Samsung Phones");
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
// only
// mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
// At the moment, this is a bit of a hack, as I'm returning ALL
// images, and just taking the latest one. There is a better way
// to
// narrow this down I think with a WHERE clause which is
// currently
// the selection variable
Cursor myCursor = this.managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try {
myCursor.moveToFirst();
imageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
// myCursor.close();
}
// Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };
String largeFileSort = MediaStore.Images.ImageColumns._ID
+ " DESC";
myCursor = this.managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";
try {
myCursor.moveToFirst();
// This will actually give yo uthe file path location of the
// image.
largeImagePath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
mImageCaptureUri = Uri.fromFile(new File(
largeImagePath));
} finally {
// myCursor.close();
}
// These are the two URI's you'll be interested in. They give
// you a
// handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(thumbnailImageId));
// I've left out the remaining code, as all I do is assign the
// URI's
// to my own objects anyways...
} catch (Exception e) {
Log.i("TAG",
"inside catch Samsung Phones exception " + e.toString());
}
try {
Log.i("TAG", "URI Normal:" + mImageCaptureUri.getPath());
} catch (Exception e) {
Log.i("TAG", "Excfeption inside Normal URI :" + e.toString());
}
//doCrop();
break;
我通过告诉相机活动将图像保存在SD卡上来解决此问题:
tempImageNameUri = "some temporary name";
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT,tempImageNameUri);
i.putExtra("return-data", true);
startActivityForResult(i,SOME_CONSTANT);
当被调用的活动完成后,onActivityResult
从临时路径中取出 SD 卡中的项目并使用它。
您可能已经找到了问题的解决方案,但这会对某人有所帮助。下面是Commonsware
创建的博客链接,该链接是关于拍摄照片后图像裁剪的。他说,使用动作com.android.camera.action.CROP
可能无法在所有不同类型的设备中完美运行。
这是链接