Android - 相机意图,返回空结果
Posted
技术标签:
【中文标题】Android - 相机意图,返回空结果【英文标题】:Android - Camera intent, returning a null result 【发布时间】:2014-04-28 17:27:21 【问题描述】:我正在尝试使用下一个代码从相机中获取图片 -
private void openImageIntent()
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
//final String fname = Utils.getUniqueImageFilename();
long elapsedMsec = System.currentTimeMillis();
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(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, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]));
startActivityForResult(chooserIntent, 1);
当 Intent 完成时,这就是代码 -
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(resultCode == RESULT_OK)
if(requestCode == 1)
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;
currImageURI = selectedImageUri;
else
selectedImageUri = data == null ? null : data.getData();
currImageURI = selectedImageUri;
现在我尝试将图像 uri 保存在一个全局变量中,命名为 *currImageURI *
我什至尝试使用这部分代码,但它似乎在破坏应用程序 -
if(isCamera)
currImageURI = data.getData();
我什至在清单中添加了下一行
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
现在从图库中选择图片一切正常,但尝试从相机中获取图片时,我得到一个空 URI。
有什么想法吗?
感谢您的帮助
【问题讨论】:
是的...您只会收到null
。您捕获的图像将保存在您作为额外参数传递的位置,键为MediaStore.EXTRA_OUTPUT
...
希望以下链接对您有用:***.com/questions/9890757/…***.com/questions/6982184/…
您是否添加了访问外部存储的权限。 (WRITE_EXTERNAL_STORAGE) ?
但似乎 [outputFileUri 是有意传递的,并在 [onActivityResult] 处被重用。是的,我用过 - WRITE_EXTERNAL_STORAGE
【参考方案1】:
我试过这段代码,我遇到了同样的问题
简单的解决方案是像这样将静态类型添加到公共变量中
private static Uri outputFileUri;
因为当Camera打开时,activity会关闭,链接从变量中删除
【讨论】:
以上是关于Android - 相机意图,返回空结果的主要内容,如果未能解决你的问题,请参考以下文章