如何从图库Android中选择多个图像

时间:2015-05-15 05:26:07

标签: android android-image android-gallery

private static final int PICK_FROM_GALLERY = 2;    
    private void fireGallerypick() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_FROM_GALLERY);
    }

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case PICK_FROM_GALLERY:
            if (resultCode == -1) {
                System.out.println("Gallery pick success");
                String[] all_path = data.getStringArrayExtra("all_path"); //But it returns null
                break;
            }
    }
}

我可以选择多个图像,当我完成选择时。我需要String[]中所有选定文件的路径。提前致谢。

1 个答案:

答案 0 :(得分:1)

Intent i = new Intent(Action.ACTION_MULTIPLE_PICK);
startActivityForResult(i, 200);

要获得onActivityResult中的图像路径,请执行此操作 -

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode == 200 && resultCode == Activity.RESULT_OK) {

            String[] all_path = data.getStringArrayExtra("all_path");



        for (String string : all_path) {

            String sdcardPath = string;


        }

注意:EXTRA_ALLOW_MULTIPLE选项仅适用于Android API 18及更高版本。