【问题标题】:Picture from gallery to an imageview图片从图库到 imageview
【发布时间】:2020-11-05 23:45:33
【问题描述】:

我正在尝试从图库中选择图像到 ImageView,但它不起作用。

代码如下:

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

这是布局:

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

这是清单:

<?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>

请让我知道问题出在哪里:) 我刚刚尝试了一些方法,但它从来没有奏效

【问题讨论】:

    标签: android android-imageview android-image


    【解决方案1】:

    发现代码中的错误:

    String filePath = null;
    

    因此您将 null 值传递给您的 DecodeFile 方法,因此您的 Bitmap 为 null。如果您想将图像设置为 ImageView,您可以这样做,而无需像这样将其转换为位图:

    YourImageView.setImageURI(selectedImageUri);
    

    【讨论】:

      【解决方案2】:
      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)
                          }
                      }
                  }
              }
          }
      

      这里是拍照和从图库功能中挑选:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-24
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多