1

Currently, I am using the Picasso library to download images and save it in the device when I press the button. the problem is when I press the button the image not download and just shown the message "Image Downloaded" , so how can i fix it? Here is my code

PicassoDisplayImageAdapter.java

/*
* This class for display the image when clicking on it
* It gets the data from the class have the images "Images in ArrayList"
* Also It is for download images
*/
public class PicassoDisplayImageAdapter extends AppCompatActivity {

public static final int PERMISSION_WRITE = 0;
String fileUri;
Button download_image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);

/* Display the data in the ImageView with Picasso "ImageView that insert in he activity" */
final ImageView imageView = findViewById(R.id.image_display);
final Intent intent = getIntent();
if (intent.hasExtra("imageUrl")){
    String url = intent.getStringExtra("imageUrl");
    Picasso.with(this)
            .load(url)
            .fit() // to resize the image to imageView
            .placeholder(R.drawable.progress_animation)
            .error(R.drawable.error)
            .into(imageView);
}

/* button to download the image */
download_image = findViewById(R.id.button_download);
checkPermission();
download_image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkPermission()) {
            String URL = intent.getStringExtra("imageUrl");
            SaveImage (URL);
        }
    }
});
}

/* method to save image*/
private void SaveImage(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        try {
            File mydir = new File(Environment.getExternalStorageDirectory() + "/11zon");
            if (!mydir.exists()) {
                mydir.mkdirs();
            }

            fileUri = mydir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
            FileOutputStream outputStream = new FileOutputStream(fileUri);

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "Image Downloaded", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
      }
  });
}

 /* runtime storage permission */
 public boolean checkPermission() {
int READ_EXTERNAL_PERMISSION = ContextCompat.checkSelfPermission(this, 
 Manifest.permission.READ_EXTERNAL_STORAGE);
  if((READ_EXTERNAL_PERMISSION != PackageManager.PERMISSION_GRANTED)) {
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
  PERMISSION_WRITE);
     return false;
 }
    return true;
 }

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==PERMISSION_WRITE && grantResults.length > 0 && grantResults[0] == 
 PackageManager.PERMISSION_GRANTED) {
    //do somethings
    }
  }
}

ImagesRamadanActivity.java That has the data

/*
* This Activity for display the ramadan images
* This class has the data of images
*/
 public class ImagesRamadanActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_ramadan_images);

/* ArrayList for RamadanImages */
final String[] RamadanImages = {
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
};

/* make new object and find the view "GridView" */
GridView gridView2 = findViewById(R.id.gridview_image_ramadan);
// display all the images from Array on it
gridView2.setAdapter(new PicassoImagesAdapter(ImagesRamadanActivity.this, RamadanImages));

/* display the image when click on it */
// we made a class for this method "the class called PicassoDisplayImageAdapter"
gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // get the image
        String image = RamadanImages[position];
        Intent intent = new Intent(ImagesRamadanActivity.this, PicassoDisplayImageAdapter.class);
        intent.putExtra("imageUrl", image);
        ImagesRamadanActivity.this.startActivity(intent);
    }
});

activity_image_display.xml activity to display the photo and has the button to download the image

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
  android:id="@+id/image_display"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#fff"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >
</ImageView>

<Button
  android:id="@+id/button_download"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="download the image"
  android:layout_alignParentBottom="true"
  android:layout_marginBottom="0dp" />

</RelativeLayout>
4
  • 1
    request this first checkPermission() inside onCreate and then proceed with your code. Commented Apr 29, 2020 at 10:42
  • Excuse me because I am still new in Android, do you mean this one inside if condition? Commented Apr 29, 2020 at 10:56
  • then you should learn basics about permission, any way you need to request first. then once you click button, check for checkPermission Commented Apr 29, 2020 at 11:01
  • Does this answer your question? how to save bitmap to android gallery Commented Oct 15, 2020 at 16:24

1 Answer 1

1

Inside,

onCreate(){
       setContentView(..);
       // requestPermission. ask for permission when app starts.
}    

        @Override
        public void onClick(View v) {
            if (checkPermission()) {
                String URL = intent.getStringExtra("imageUrl");
                SaveImage (URL);
            }
        }

// kind of this, add this block to your existing code.

  private void requestPermission() {
      if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
         Toast.makeText(MainActivity.this, "Write External Storage permission allows us to save files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
      } else {
         ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
      }
   }

// make sure Manifest has all the permission defined

Sign up to request clarification or add additional context in comments.

1 Comment

Well, I added this code in the MainActivity and indeed when I enter the program the message appears but when I go to the pictures and download the image also the image is not saved in the device, the same problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.