gpt4 book ai didi

java - requestCode = -1 和 resultCode = 0 chooserIntent 来自画廊或相机的图像

转载 作者:行者123 更新时间:2023-11-29 20:54:23 25 4
gpt4 key购买 nike

所以我设法从 this 创建了一个 chooserIntent 复制和粘贴链接 赏金 答案。我面临的问题是在 onActivityResult 方法中。我收到的requestCode是-1,resultCode是0,代码有什么问题?

起始 Intent :

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 = "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, 0);
}

onActivityResult:

@Override
protected void onActivityResult(int resultCode, int requestCode, Intent returnIntent) {
super.onActivityResult(resultCode, requestCode, returnIntent);
if(requestCode == 0) {
if(resultCode == RESULT_OK) {
final boolean isCamera;
if(returnIntent == null)
{
isCamera = true;
}
else
{
final String action = returnIntent.getAction();
if(action == null)
{
isCamera = false;
}
else
{
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}

Uri selectedImageUri;
if(isCamera)
{
selectedImageUri = outputFileUri;
ivProfilePicture.setImageURI(selectedImageUri);
makeToast(selectedImageUri.toString(), false);

}
else
{
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
makeToast(selectedImageUri.toString(), false);
}
}
}
}

最佳答案

RESULT_OK=-1 根据 Activity 定义。您将请求代码指定为 0,这样看起来也不错。您的问题:您混淆了为 the Activity class 指定的 requestCode 和 resultCode ..试试这个:

protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent)

关于java - requestCode = -1 和 resultCode = 0 chooserIntent 来自画廊或相机的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28196291/

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