Steve
Steve

Reputation: 1153

Selected Gallery image not showing in imageview

I was selected the image after it have to set it in the imageview.But it doesn't set it in imageview.

I have posted the relevant code.

EditViewProfileActivity.java

public class EditViewProfileActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

  private static final int CAMERA_PICTURE = 1;
    private static final int GALLERY_PICTURE = 2;
    File destination = null;
    String filePath = null;
    Bitmap chosenImage;
    String imgSelected = null;

 ImageView dateBirthImg;


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_view_profile_activity);

   galImageView = (ImageView) findViewById(R.id.gallery_image_edit_view);

galImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                choosePictureAction();
            }
        });

}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



            if (requestCode == GALLERY_PICTURE
                && resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String[] projection = {MediaStore.MediaColumns.DATA};
            Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            String selectedImagePath = cursor.getString(column_index);
            destination = new File(selectedImagePath);
            filePath = selectedImagePath;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(selectedImagePath, options);
            final int REQUIRED_SIZE = 200;
            int scale = 1;
            while (options.outWidth / scale / 2 >= REQUIRED_SIZE
                    && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;
            options.inSampleSize = scale;
            options.inJustDecodeBounds = false;
            chosenImage = BitmapFactory.decodeFile(selectedImagePath, options);


            if (chosenImage != null) {
                imgSelected = "fulFilled";
                galImageView.setImageBitmap(chosenImage);

                Log.e("ChosenImage", "" + chosenImage);

            }


        } else if (requestCode == GALLERY_PICTURE
                && resultCode == RESULT_CANCELED) {

        }
    }


    private void choosePictureAction() {

        final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(EditViewProfileActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (items[which].equals("Camera")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, CAMERA_PICTURE);
                } else if (items[which].equals("Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, GALLERY_PICTURE);
                } else if (items[which].equals("Cancel")) {
                    dialog.dismiss();
                }

            }
        });

        builder.show();

    }


}

I dont know why selected image not showing in imageview.Anyone can help me with this.Thank you.

Upvotes: 1

Views: 3704

Answers (2)

Shahbaz Hashmi
Shahbaz Hashmi

Reputation: 2815

If you only want to set image in imageview. you can directly use the uri. Like this

String stringUri = selectedImageUri.getPath();
galImageView.setImageURI(null); 
galImageView.setImageURI(Uri.parse(stringUri));

Upvotes: 1

user5248371
user5248371

Reputation:

I Have use this code:

private void openGallery() {
    Intent gallery =
            new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
            Uri imageUri = data.getData();

            try {
                bitmaps = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

                final ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmaps.compress(Bitmap.CompressFormat.PNG, 90, stream);
                byte[] byteArray = stream.toByteArray();

                 encodeded = Base64.encodeToString(byteArray, Base64.DEFAULT);

                byte[] decodedString = Base64.decode(encodeded, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                userinfoimage.setImageBitmap(bitmaps);

                new UploadImage().execute();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }

Upvotes: 2

Related Questions