2

I've tried every answer I've found for this issue without success. My users choose an image from their gallery to be displayed in an ImageView using Picasso, but it never get's loaded into the imageview.

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

            String selectedImageURI = data.getData().getPath();
            File imageData = new File(String.valueOf(selectedImageURI));
            //selectedImagePath = getPath(selectedImageURI);

            if(!image1_exists){
                Picasso.with(MemoriesActivity.this).load(imageData).noPlaceholder().centerCrop().fit().into(image1);
            }
            else if(!image2_exists){
                //This is executed
                Picasso.with(MemoriesActivity.this).load(imageData).noPlaceholder().centerCrop().fit().into(image2);
            }
            else if(!image3_exists){
                //Picasso.with(MemoriesActivity.this).load(imageData).centerCrop().fit().into(image3);
            }
        }

    }
}

public void addPhoto(View v){

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

}

I can't find any explainable cause for this except something must be wrong with onActivityResult together with Picasso, because I load images using exactly the same Picasso build/method when fetching images from my backend.

EDIT SOLUTION

public void attachImage(Uri uri, int imageIndex) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 0;

    InputStream stream;
    try {

        stream = getContentResolver().openInputStream(uri);

        Bitmap bitmap = BitmapFactory.decodeStream(stream);
        Bitmap image_scaled = Bitmap.createScaledBitmap(bitmap, 800, 800 * bitmap.getHeight() / bitmap.getWidth(), false);


        if (imageIndex == 0) {
            uploadImage(image_scaled, "image1");
            handler.get(imageIndex).loadImageFromUri(this,(ImageView) findViewById(R.id.image1),uri);
            handler.get(imageIndex).loadEnlargedImageFromUri(this,image1_full,uri);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

So I created that method (it's under construction, but the solution remains the same) and calls it like this in onActivityResult :

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

            Uri selectedImage = data.getData();

            int imageIndex = getFirstNonExistingImage();

            if (imageIndex == 0) {
                attachImage(selectedImage,imageIndex);
            }

        }

    }
}

I load the URI with a custom Picasso class (handler.loadImageFromUri), but same principle.

2
  • Try converting the file to a URI
    – Triode
    Apr 5, 2016 at 12:16
  • @Triode I have tried. The method of loading isn't wrong as I've tried every single possible solution for that.
    – Knut
    Apr 5, 2016 at 14:44

3 Answers 3

1

Solved by creating an external method and executing that inside onActivityResult instead. No idea why, but it worked.

4
  • I'm trying to get the same idea for my app and want to use Picasso to cut the coding in half with the API doing to caching. Did you mean you made an external method and moved the code that adds the images into it while calling it from the onActivityResult method? Thanks :)
    – SmiffyKmc
    May 3, 2016 at 18:49
  • @SmiffyKmc provided my solution in an Edit to the original post for you! Hope it helps.
    – Knut
    May 4, 2016 at 6:09
  • Thanks a million :). Managed to follow what thou said in the above answer and got it working :). Great work with it! 👍
    – SmiffyKmc
    May 4, 2016 at 6:53
  • Glad I could help @SmiffyKmc :)
    – Knut
    May 4, 2016 at 9:04
0

get real path from URI and set it to Picasso.

 Uri selectedImage = data.getData();

 imagePath = getRealPathFromURI(this, selectedImage);

 public String getRealPathFromURI(Context context, Uri contentUri) {

 Cursor cursor = null;

 try {

  String[] proj = {MediaStore.Images.Media.DATA};

   cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
1
  • it made no difference my friend.
    – Knut
    Apr 5, 2016 at 13:52
0

You dont need to make cursor query with picasso.

With picasso you just need to write one line of code I have extended it for better understanding :-

           //Get image Uri
            Uri selectedImageURI = data.getData();

            //Load image from picked Uri
Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
                    .into((ImageView) findViewById(R.id.imageView1));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.