如何在onActivityResult中接收多个图库结果

时间:2010-10-06 15:40:56

标签: android android-intent gallery

我想通过意图启动图库,并希望在我的onActivityResult方法中获得结果。 如果我点击一张照片,一切正常。 我现在得到的问题是,如果我在画廊中选择多张图片,我如何得到结果?

如果找到hcpl的代码,它会为一张图片执行:

public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

1 个答案:

答案 0 :(得分:0)

您需要将该图像路径存储到数组字符串中,然后根据您的要求进行检索。

下面是一个示例代码,它会弹出一个带有3个选择标准的对话框,如相机,图库,设备。 这可能会对你有帮助。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;




public class FileAttachPopUpUtils extends Activity {

    private View rootView;
    private Dialog popup;
    private Button Assig_PopUp_DeviceBtn;
    private Button Assig_PopUp_GalaryBtn;
    private Button Assi_PopUp_CameraBtn;
    private Button Assig_PopUp_CancelPopupBtn;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.assignment_popupfile);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        /*
         * getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
         * WindowManager.LayoutParams.FLAG_FULLSCREEN);
         */
        popup = new Dialog(this);
        // popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // popup.getWindow().setBackgroundDrawable(new
        // ColorDrawable(android.graphics.Color.TRANSPARENT));
        popup.setContentView(R.layout.assignment_popupfile);

        popup.setCanceledOnTouchOutside(false);

        popup.show();

        Assig_PopUp_DeviceBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_DeviceBtn);

        Assig_PopUp_GalaryBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_GalaryBtn);
        Assi_PopUp_CameraBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_CameraBtn);

        Assig_PopUp_CancelPopupBtn = (Button) popup
                .findViewById(R.id.Assignment_PopUp_CancelPopupBtn);

        Assig_PopUp_DeviceBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                /*
                 * Toast.makeText(FileAttachPopUpUtils.this,
                 * "Device File In-Progress", Toast.LENGTH_SHORT) .show();
                 */
                Intent intent = new Intent(FileAttachPopUpUtils.this,
                        FileExplore.class);
                startActivity(intent);
                popup.dismiss();
                finish();

            }
        });

        Assig_PopUp_GalaryBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 2);

            }
        });
        Assi_PopUp_CameraBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                // ///////////////////////////////////////////////////////////////

                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            }
        });

        Assig_PopUp_CancelPopupBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                popup.dismiss();
                FileAttachPopUpUtils.this.finish();
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == this.RESULT_OK) {
            if (requestCode == 1) {

                /*
                 * Intent intent = new Intent( Intent.ACTION_PICK,
                 * android.provider
                 * .MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 * startActivityForResult(intent, 2);
                 */

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                // mImage.setImageBitmap(thumbnail);
                // 3
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                // 4
                String name;
                int n = 100000;
                int rand;
                rand = new Random().nextInt(n);
                name = "Image-" + rand + ".jpg";
                // File fileimage = new File(path, name);
                File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + name);

                System.out.println("FILE PATH=======>>>>>>>>>>"
                        + file.toString());
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    // 5
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Assignments.mulPathArry.add(file.toString());
                // Here Your Storing the image Path each time it calls
                popup.dismiss();
                FileAttachPopUpUtils.this.finish();

            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = this.getContentResolver().query(selectedImage,
                        filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                System.out
                        .println("path of image from gallery......******************........."
                                + picturePath);



                Assignments.mulPathArry.add(picturePath);
                // Here Your Storing the Image Path when each time it calls
                popup.dismiss();
                FileAttachPopUpUtils.this.finish();
            }
        }

        // ///////////////////////////////////////////////////////////////////////

    }

    @Override
    public void onBackPressed() {
        return;
    }

}