简体   繁体   English

图片从图库到图像视图

[英]Picture from gallery to an imageview

i'm trying to select an image from gallery to an ImageView but it doesen't work.我正在尝试从图库中选择图像到 ImageView,但它不起作用。

Here's the code:这是代码:

public class Profilo extends MainActivity {

private ImageView img;
private Bitmap bmp;

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

    img = (ImageView)findViewById(R.id.profile_image);

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 1);
        }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                String filePath = null;
                if (requestCode == 1) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    filePath = cursor.getString(columnIndex);
                    cursor.close();

                    if (bmp != null && !bmp.isRecycled()) {
                    }
                    bmp = null;
                }
                bmp = BitmapFactory.decodeFile(filePath);
                img.setBackgroundResource(0);
                img.setImageBitmap(bmp);
            }
            else
            {
                Log.d("Status:", "Photopicker canceled");
            }
         }
    });
}

Here's the layout:这是布局:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/profile_image"
    android:contentDescription="@string/profile_image"
    android:src="@drawable/profilo_facebook"
    android:layout_gravity="center_horizontal"/>

</LinearLayout>

and here's the MANIFEST:这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sidacri.testapp" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
<activity android:name=".Profilo"
        android:label="@string/label_profilo">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>
</manifest>

Let me know where's the problem, please :) i've just tried some methods but it never doesen't work请让我知道问题出在哪里:) 我刚刚尝试了一些方法,但它从来没有不起作用

Got the bug in your code:在您的代码中发现错误:

String filePath = null;

so you are passing the null value to your DecodeFile method and so your Bitmap is null.因此您将空值传递给您的DecodeFile方法,因此您的图为空。 If you want to set the image to ImageView,you can do that without converting it to Bitmap like this:如果要将图像设置为 ImageView,则无需将其转换为 Bitmap 即可,如下所示:

YourImageView.setImageURI(selectedImageUri);
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data != null) {
            when (requestCode) {
                Constants.REQUEST_CODE_CAMERA -> {
                    val image: Bitmap = data.extras?.get("data") as Bitmap
                    binding.profilePhoto.setImageBitmap(image)
                }
                Constants.REQUEST_CODE_GALLERY -> {

                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P){
                        val source = ImageDecoder.createSource(
                            (activity as MainActivity).contentResolver,
                            data.data!!
                        )
                        val bitmap = ImageDecoder.decodeBitmap(source)
                        binding.profilePhoto.setImageBitmap(bitmap)
                    } else {
                        val bitmap = MediaStore.Images.Media.getBitmap((activity as MainActivity).contentResolver, data.data)
                        binding.profilePhoto.setImageBitmap(bitmap)
                    }
                }
            }
        }
    }

Here is take photo and pick from gallery functions:这是拍照并从画廊功能中挑选:

    private fun openCamera() {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        startActivityForResult(intent, Constants.REQUEST_CODE_CAMERA)
    }
    private fun choosePhotoFromGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, Constants.REQUEST_CODE_GALLERY)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在ImageView中显示从图库导出的图片 - Cannot show in ImageView a picture exported from the gallery 在ImageView中不显示图片库 - In ImageView doesn't show picture from gallery 检查imageview中的图片是否从相机或图库中捕获 - Check if picture in imageview was captured from camera or gallery 从图库Android Apps拍照时,ImageView不显示图像 - ImageView not display an image when taking a picture from the gallery Android Apps 点击“上传图片”后从图库功能中选择图片并在 imageView 中显示图片 - Select picture from gallery function after hitting the 'UPLOAD PICTURE' and display the picture in imageView 从图库中获取图片 - Fetching a picture from Gallery 如何从图库中选择图片并使用SQLite保存为背景ImageView - How do choose picture from gallery and save as background ImageView, using SQLite 如何recyclerview适配器imageview选择打开相机获取图像并从照片库中选择一张图片? - how to recyclerview adapter imageview select open camera get image and choose a picture from the photo gallery? 如何通过单击导航抽屉上的用户个人头像的imageview从图库中选择图像 - how to select the image from gallery by clicking on imageview on navigation drawer for user profile picture 在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸 - Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM