所以情况是我有这个按钮,它将调用以下函数以允许您选择一个应用程序作为源(相机和图库应用程序)。但是,我想添加另一个我创建的将从谷歌图像中选择的图像。我拥有获取谷歌图片的所有工作代码,但如何将其添加到此列表中?我一直在寻找所有错误的地方。任何方向都会有所帮助!
private static void openImageIntent(Activity a) {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "photo";
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 = a.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[]{}));
a.startActivityForResult(chooserIntent, GALLERY_PIC_REQUEST);
}
------------------------编辑-------------------- ------ 我明白了!
<activity
android:name="com.projectCaruso.imagesearch.ImageGridActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
最佳答案
好吧,我不是这方面的专家,但我想您会在此处列出的示例中找到答案:http://developer.android.com/guide/components/intents-common.html#Storage
简而言之,您需要扩展自定义“Google 图片 Activity ”的 list ,以响应您正在创建的 Intent
的操作和数据 mime 类型。也许它可能看起来像这样,但再次重申:我不是专家,我自己还没有尝试过:
<activity ...>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<data android:type="image/*" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
</intent-filter>
</activity>
文档指出,如果没有 OPENABLE
类别,您将无法实现此功能,这会强制您通过“支持 OpenableColumns< 的特殊
和 ContentProvider
使文件可用ContentResolver.openFileDescriptor()
”。实际上,您可能需要编写一些粘合代码来满足此要求。
关于android - 添加另一个 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957132/