2

This contents of this loadImage method never gets called although I am calling this method in OnActivityResult.

public void loadImage(Uri uri){
    File file = new File(getApplicationContext().getExternalCacheDir().getAbsolutePath() + "/" +uid + ".jpg");
    Log.d("Check Bitmap", "file" + file);
    try {
        Picasso picasso = Picasso.get();
        Bitmap bitmap = picasso.load(uri).get();
        Log.d("Check Bitmap", "bitmap working");
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100,new FileOutputStream(file));
    } catch (Exception e) {
        Log.d("Check Bitmap", "bitmap not working, cached");
        e.printStackTrace();
    }

My aim is to retrieve the image from the file.

This is my OnActivityResult

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==1000){
            {
                if(resultCode == Activity.RESULT_OK){
                    imageUri = data.getData();
                    Context context;
                    final ProgressDialog dialog = new ProgressDialog(myProfile.this);
                    dialog.setMessage("Uploading Image...");
                    dialog.show();
                    profilepic.setImageURI(imageUri);

                    ref.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                    ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                        @Override
                                        public void onSuccess(Uri uri) {
                                            final Picasso picasso = Picasso.get();
                                            picasso.setIndicatorsEnabled(true);
                                            picasso.load(uri).into(profilepic);
                                            downloadUri = uri;
                                            dialog.dismiss();
                                            Toast.makeText(getApplicationContext(), "Image Uploaded!", Toast.LENGTH_SHORT).show();


                                            loadImage(uri); //Here I call
                   
miraquee
  • 181
  • 10
  • which logs do you see? can you post also your `onCreate` methods? (where do you call `loadImage`) – snachmsm Nov 09 '20 at 07:03
  • Actually I am calling the method in **OnActivityResult**, I previously wrote **OnCreate** for simplicity. Though I hope it would not affect anything as the toast is working fine and all other lines are also executing fine. – miraquee Nov 09 '20 at 07:16
  • And my Logs do not show as expected by the line in **loadImage** method. – miraquee Nov 09 '20 at 07:17

1 Answers1

1

You should not use space separated TAG for logs. Try replacing your "Check Bitmap" with something of one word (eg. "Checkere"). Then you will be able to see your Log in your Logcat. You need to put everything inside a thread.

Here is the complete block of code for loadImage

 public void loadImage(final Uri uri){
        Thread thread = new Thread() {
            @Override
            public void run() {
                Log.d("Checkere", "stuck at file");
                File file = new File(getCacheDir() + File.separator + uid + ".jpg");
                Log.d("Checkere", "file" + file);
                try {
                    Picasso picasso = Picasso.get();
                    Bitmap bitmap = picasso.load(uri).get();
                    Log.d("Checkere", "bitmap working");
                    FileOutputStream fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception e) {
                    Log.d("Checkere", e.getMessage());
                    e.printStackTrace();
                }
            }};
        thread.start();
    }

Note that I have also added these two lines in your code.

fOut.flush(); 
fOut.close();

I hope this helps!

Arpit Anand
  • 347
  • 2
  • 17