问题描述
在我的应用程序中,单击按钮后,我想显示应用程序中可用照片应用程序的选择器。 目的是从与照片相关的应用程序中选择一个图像,并将其显示在我的应用程序中。
单击按钮后,正在执行以下方法:
private void fireIntentToOpenDeviceImageResources() {
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContext().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent, takePicture});
startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST);
}
问题是在创建的选择器中没有看到保管箱应用程序(确定已安装在测试手机中)。 我应该添加哪种意图才能包含Dropbox应用程序?
编辑:
我添加了这样的意图:
PackageManager manager = getActivity().getPackageManager();
Intent i = manager.getLaunchIntentForPackage("com.dropbox.android");
i.setAction(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
现在,它在选择器中显示了保管箱应用程序,但我无法从中选择图像。 它只是启动了保管箱应用程序,而没有选择并返回我的应用程序的可能性。
1楼
Android 4.4(API级别19)引入了 ,具有集中式文档访问权限。 您可以使用以下简单代码打开文档选择器:
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
startActivityForResult(getIntent, MY_REQUEST_CODE);
因此,在Android> = 19上,您的代码将使用各种应用程序打开意图选择器,例如Camera
, Photos
(等,等等……取决于所安装的应用程序)和Documents
。
如果您选择“ Documents
则文档选择器将打开,并且在侧栏上,设备中安装了所有不同的应用程序,包括Dropbox。
如果您希望Dropbox直接在意图选择器中可用,则可以这样更改代码:
private void fireIntentToOpenDeviceImageResources() {
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
Uri imageUri = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
Intent dropboxIntent = new Intent(Intent.ACTION_GET_CONTENT);
dropboxIntent.setPackage("com.dropbox.android");
dropboxIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent});
startActivityForResult(chooserIntent, GlobalConsts.PICK_IMAGE_REQUEST);
}
警告!
使用这种技术,您需要手动添加仅在文档选择器中可用的每个应用程序,例如,如果您还想添加ES File Manager,则需要创建意图并添加到EXTRA_INITIAL_INTENTS
列表中:
Intent esFileManagerIntent = new Intent(Intent.ACTION_GET_CONTENT);
esFileManagerIntent.setPackage("com.estrongs.android.pop");
esFileManagerIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePicture, dropboxIntent, esFileManagerIntent});