我有一项活动允许用户选择预览他们从图库或相机中选择的照片。我遇到的问题是相机/图库意图立即返回,然后显示相机/图库并且不返回任何内容。
事物的基本流程如下:片段 - >申请子类 - >热门活动 - (startActivity) - >照片预览活动 - (在onCreate中) - >照片选择器意图
//In the application subclass
public static void launchImageSelector()
{
if(!(topActivity instanceof ImagePreviewActivity))
{
Intent i = new Intent(context, ImagePreviewActivity.class);
topActivity.startActivityForResult(i, kImageSelectorRequestCode);
}
}
///in ImagePreviewActivity class
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent chooser = createChooserIntent(createCameraIntent());
chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent("image/*"));
startActivityForResult(chooser, 1);
}
//intent creaters(from android src)
private Intent createChooserIntent(Intent... intents)
{
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "Choose Photo");
return chooser;
}
private Intent createOpenableIntent(String type)
{
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
// i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(type);
return i;
}
private Intent createCameraIntent()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + "browser-photos");
cameraDataDir.mkdirs();
String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
photoFileUri = Uri.fromFile(new File(mCameraFilePath));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFileUri);
return cameraIntent;
}
我在这里做错了什么?什么会导致Chooser Intent立即返回但是还会继续?我在这里做了一些根本错误的事情吗?
感谢您的帮助!!
答案 0 :(得分:4)
经过数小时的调试后,问题出现在Manifest文件中。在android中,如果启动模式设置为singleInstance或singleTop
,则无法为结果启动活动在这里找到答案:Android - startActivityForResult immediately triggering onActivityResult