Android - 如何创建图像选择器

发布于 2024-12-28 20:52:17 字数 96 浏览 0 评论 0原文

在我的应用程序中,我需要使用一些东西来让用户从设备中选择图像。
简单的方法是如何做到这一点?

请记住,我将在选择后使用该图像。

提前致谢。

In my app i need to use something that will let the user select an image from device.
How is the simple way to do this?

Remembering that i'll use that image after selected.

Thanks in advance.

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

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

发布评论

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

评论(2

萤火眠眠 2025-01-04 20:52:17

您可以尝试以下操作,打开图库并让用户选择图像。

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

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
    }
}

'selectedImage '

是选定的图像,因此您现在可以在应用程序的其余部分中使用它。

You can try the following which opens the gallery and lets user pick an image.

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

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
    }
}

}

'selectedImage' is the selected image, so you can use it in the rest of your application now.

三生池水覆流年 2025-01-04 20:52:17

使用此代码从图库中选择图像

galleryPic.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Util.DogDye = false;
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    1);

        }
    });

,然后添加一种新方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if (resultCode != RESULT_OK) return;

    switch (requestCode) {

    case 1:
        if (data != null) {
            selectedImageUri = data.getData();
            String selectedImagePath = getPath(path);
BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
Bitmap btemp = BitmapFactory.decodeFile(selectedImagePath,
                        options);
          /// use btemp Image file 

        }
        break;
    }

}


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

use thi code to select image from Gallery

galleryPic.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Util.DogDye = false;
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    1);

        }
    });

and after this add one new method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if (resultCode != RESULT_OK) return;

    switch (requestCode) {

    case 1:
        if (data != null) {
            selectedImageUri = data.getData();
            String selectedImagePath = getPath(path);
BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
Bitmap btemp = BitmapFactory.decodeFile(selectedImagePath,
                        options);
          /// use btemp Image file 

        }
        break;
    }

}


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