开发者

How to receive multiple gallery results in onActivityResult

I want to start the gallery via intent and want to get the results in my onActivityResult method. All wor开发者_开发知识库ks fine if i click on one pic. The question ive got now is, how do i get the results, if i choose more than one picture in the gallery?

If found hcpl´s code, which does it for one picture:

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);
}


You need to store that image path into array string then retrieve it according to your requirement.

Here is a sample code which will Pop up one Dialog box with 3 selection criteria like camera, Gallery, Device. This may help you.

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;
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜