我正在使用以下代码从相机获取图片。除了三星,它在其他手机上运行良好。请让我知道我做错了什么。
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "temp" + File.separator);
root.mkdir();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = cordova.getActivity().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/jpeg");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, PICK_FILE_REQUEST);
以及活动结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FILE_REQUEST) {
if(data!=null){
Logv("Data","got some data");
}
}
}
最佳答案
除了三星,它在其他手机上运行良好
不,它只在您尝试过的设备上使用相机应用程序。它通常不适用于ACTION_IMAGE_CAPTURE
。
请让我知道我做错了什么
您假设ACTION_IMAGE_CAPTURE
应该返回一个结果Intent
。It is not required when you provide EXTRA_OUTPUT
。您知道您要求将图像保存到哪里(您提供给EXTRA_OUTPUT
的值),因此请在那里查找图像。
请注意,有些有问题的相机应用程序会忽略EXTRA_OUTPUT
(将图像存储在它们想要的位置),因此,如果您的ACTION_IMAGE_CAPTURE
请求中的图像不在EXTRA_OUTPUT
中,则您将失去运气。