0

i'm trying to select an image from gallery to an ImageView but it doesen't work.

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

mmBs
  • 8,421
  • 6
  • 38
  • 46
invisible142
  • 33
  • 1
  • 3

2 Answers2

0

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. If you want to set the image to ImageView,you can do that without converting it to Bitmap like this:

YourImageView.setImageURI(selectedImageUri);
Pankaj
  • 2,115
  • 2
  • 19
  • 39
0
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)
    }
Atakan Akar
  • 133
  • 1
  • 7