2015-10-19 54 views
1

在我的应用程序按钮点击后,我想显示在我的应用程序中选择可用的照片应用程序。我们的目标是从其中一个与照片相关的应用中选择图片,并将其显示在我的应用中。意图打开保管箱在Android中选择图像

按钮点击下面的方法后,正在执行:

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的应用程序(这是肯定的安装在我的测试手机)中创建选择器。我应该添加什么样的意图,以包括Dropbox应用程序?

编辑:

我说这样的意图:

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

现在它显示在选配的Dropbox应用程序,但我不能从中挑选图像。它只是启动Dropbox应用程序而无需选择并返回到我的应用程序的可能性。

+0

http://stackoverflow.com/questions/18870169/pick-image-from-gallery-camera-dropbox-etc – Pavan

回答

4

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你的代码将打开一个包含各种应用目的选择器,例如CameraPhotos(等等,等等...取决于安装的应用程序)和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中,你需要创建的意图,并添加到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}); 
+0

伟大的信息。你的回答有助于解决我的问题。谢谢! – user2999943