从图库中选择多个图像

时间:2014-10-31 04:55:30

标签: android image select android-intent

我正在尝试从Android画廊中选择图片。这是我的代码。它适用于单张图像。但是,如果选择多个图像,它给我回null。任何想法都出错了

Button addNewCart = (Button) findViewById(R.id.imageSelect);
        addNewCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent( );
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    startActivityForResult(Intent.createChooser(intent,
                            "select multiple images"), 100);
            }
        });

以下是活动代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK){
        String[] all_path = data.getStringArrayExtra("all_path");
        if(data != null)
        {   
            Uri selectedImageUri = data.getData();
            System.out.println(selectedImageUri);

        }
    }
}

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

尝试这样,

private final int PICK_IMAGE_MULTIPLE =1;

 addNewCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent intent = new Intent( );
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                        startActivityForResult(Intent.createChooser(intent,
                                "select multiple images"), PICK_IMAGE_MULTIPLE);
                }
            });

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_OK){
     if(requestCode == PICK_IMAGE_MULTIPLE){
            String[] imagesPath = data.getStringExtra("data").split("\\|");
    }
        }
    }