java - Intent 打开 dropbox 以在 Android 中选择图像

标签 java android android-intent android-activity

在我的应用程序中单击按钮后,我想在我的应用程序中显示可用照片应用程序的选择器。目标是从其中一个与照片相关的应用程序中挑选图像并将其显示在我的应用程序中。

点击按钮后执行以下方法:

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

问题是我在创建的选择器中没有看到保管箱应用程序(它肯定安装在我的测试手机中)。我应该添加什么样的 Intent 才能包含 Dropbox 应用程序?

编辑:

我添加了这样的 Intent :

PackageManager manager = getActivity().getPackageManager();
Intent i = manager.getLaunchIntentForPackage("com.dropbox.android");
i.setAction(Intent.ACTION_GET_CONTENT);       
getIntent.setType("image/*");

现在它在选择器中显示 Dropbox 应用程序,但我无法从中选择图像。它只是启动 Dropbox 应用程序,而无需选择并返回到我的应用程序选项。

最佳答案

Android 4.4(API 级别 19)引入了 Storage Access Framework (SAF) ,具有集中的文档访问权限。您可以使用以下简单代码打开文档选择器:

Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
startActivityForResult(getIntent, MY_REQUEST_CODE);

出于这个原因,在 Android >=19 上,您的代码将使用各种应用程序打开 Intent 选择器,例如 CameraPhotos(等等,等等...取决于安装了哪些应用程序)和 Documents。如果您选择 Documents,文档选择器将打开,并且在边栏上会显示您设备中安装的所有不同应用程序,包括 Dropbox。

如果您希望 Dropbox 直接在 Intent 选择器中可用,您可以像这样更改代码:

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 文件管理器,则需要创建 Intent 和添加到 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});

关于java - Intent 打开 dropbox 以在 Android 中选择图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215972/

相关文章:

android - Windows Phone 的 native iOS/android 应用程序

android - 当 launchMode ="singleTask"重新打开 Activity 时,返回堆栈会发生什么情况?

php - Android 从 PHP 传递和接收参数

java - 返回 Java 流中的第一个结果匹配谓词或所有非匹配结果

java - 我想放置一个触摸事件来开始我的游戏

android - 如何从状态栏检测方向变化

android - 包裹 : unable to marshal value - Fragments

java - 从文本框传递参数

java - JFreeChart Java JAR 未在不同的计算机上运行

java 路径未指向 Windows 中的 sdk 路径