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

发布于 2024-09-27 00:22:40 字数 1623 浏览 6 评论 0原文

我想通过意图启动图库并希望在 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);
}

I want to start the gallery via intent and want to get the results in my onActivityResult method.
All works 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-10-04 00:22:40

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

下面是一个示例代码,它将弹出一个对话框,其中包含 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;
    }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文