【问题标题】:Android Chooser Intent for Camera Gallery AND Documents用于相机库和文档的 Android 选择器意图
【发布时间】:2016-09-15 04:50:15
【问题描述】:

我在创建允许用户选择相机应用、图库应用或文件浏览器应用的选择器意图时遇到问题。

到目前为止,我只能获得相机 + 画廊或相机 + 文件浏览器,但不是全部 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);
}

【问题讨论】:

    标签: android android-intent camera gallery documents


    【解决方案1】:

    嗯,我想通了。 看起来每次调用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多