gpt4 book ai didi

Android - 相机 Intent ,返回空结果

转载 作者:太空狗 更新时间:2023-10-29 15:05:13 26 4
gpt4 key购买 nike

我正在尝试使用下一个代码来从相机中获取照片 -

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();

}

我什至在 list 中添加了下一行

        <uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

现在从图库中挑选图片一切正常,但是当尝试从相机中获取图片时,我得到一个空 URI。

有什么想法吗?

感谢您的帮助

最佳答案

我试过这段代码,我遇到了同样的问题

简单的解决方案是像这样向公共(public)变量添加静态类型

private static Uri outputFileUri;

因为当相机打开时, Activity 将关闭并且链接从变量中删除

关于Android - 相机 Intent ,返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22557183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com