所以我设法创建了一个选择器内容复制和粘贴,从this链接赏金奖励答案。我面临的问题是onActivityResult
方法。我收到的requestCode是-1,resultCode是0.代码有什么问题?
开始意图:
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);
}
}
}
}
答案 0 :(得分:2)
RESULT_OK=-1
。您将请求代码指定为0,因此看起来也很好。您的问题:您正在混合requestCode和resultCode,正如the Activity class所指定的那样。试试这个:
protected void onActivityResult(int requestCode,int resultCode,Intent returnIntent)