Android选择器意图用于相机库和文档

时间:2016-09-15 04:50:15

标签: android android-intent camera gallery documents

我无法创建选择器意图,允许用户选择相机应用程序,图库应用程序或文件浏览器应用程序。

到目前为止,我只能获得相机+图库或相机+文件浏览器,但不能获得所有3个选项

在下面的方法中,似乎忽略了以下行:

chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntent);

代码:

public void openUploadIntent() {

    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String fname = "ABCD_" + timeStamp;

    final File sdImageMainDirectory = new File(storageDir, 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 = 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);
    }


    //Gallery.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    //Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);


    // Filesystem.
    final Intent fsIntent = new Intent();
    fsIntent.setType("*/*");
    fsIntent.setAction(Intent.ACTION_GET_CONTENT);


    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(fsIntent, "Select Source");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntent);

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.ize()]));

    startActivityForResult(chooserIntent, 99999);
}

1 个答案:

答案 0 :(得分:3)

好吧我明白了。 看起来每次调用EXTRA_INTIAL_INTENTS都会替换掉最后一个。 这是我的最终工作代码(在三星Galaxy S上)

public void openImageIntent() {

    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String fname = "ABCD_" + timeStamp;

    final File sdImageMainDirectory = new File(storageDir, 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 = 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);
    }


    //Gallery.
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    // Filesystem.
    final Intent fsIntent = new Intent();
    fsIntent.setType("*/*");
    fsIntent.setAction(Intent.ACTION_GET_CONTENT);
    cameraIntents.add(fsIntent);



    //Create the Chooser
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, 99999);
}