Add*_*dev 11 java android android-intent
我正试图发起一个意图从相机或Android的画廊中选择一个图像.我检查过这篇文章,目前我的代码即将开始工作:
private Intent getPickIntent() {
final List<Intent> intents = new ArrayList<Intent>();
if (allowCamera) {
setCameraIntents(intents, cameraOutputUri);
}
if (allowGallery) {
intents.add(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
}
if (intents.isEmpty()) return null;
Intent result = Intent.createChooser(intents.remove(0), null);
if (!intents.isEmpty()) {
result.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[] {}));
}
return result;
}
private void setCameraIntents(List<Intent> cameraIntents, Uri output) {
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = context.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, output);
cameraIntents.add(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
当我设置allowCamera=true它正常工作.
当我设置allowGallery=true它时显示以下选择器:

但如果我设置allowCamera=true并且显示allowGallery =true的选择器是:

如果您选择,Android System则会显示第一个选择器.
我希望选择器是这样的:

我如何"扩展" Android System选项?
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT, null);
galleryintent.setType("image/*");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryintent);
chooser.putExtra(Intent.EXTRA_TITLE, "Select from:");
Intent[] intentArray = { cameraIntent };
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser, REQUEST_PIC);
Run Code Online (Sandbox Code Playgroud)
在您的链接帖子中,您可以找到解决方案.您的代码的不同之处在于如何创建图库意图:
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
Run Code Online (Sandbox Code Playgroud)
不是ACTION_PICK你怎么做但是ACTION_GET_CONTENT.似乎如果ACTION_PICK列表中有一个单独的("容器意图"),系统会遍历它以显示选择内容,但是一旦你包含相机意图它就不能再遍历了(因为有一个直接意图和一个容器意图).
在评论这个答案你会发现之间的差异ACTION_PICK和ACTION_GET_CONTENT.
有一些解决方案建议使用自定义对话框.但是这个对话框没有标准图标(参见这里的 develper文档).所以我建议保持你的解决方案,并解决hierarchie问题.
| 归档时间: |
|
| 查看次数: |
8081 次 |
| 最近记录: |