I am trying to make a chat app. If a user sends a picture to me, I want to download that picture in my phone's internal storage. I used the URL of the image stored in the Firebase Storage to show it on my app via Gradle, but I don't understand how can I download this image in my own personal storage. I want the user to click a button on the app and be able to download the image that is being displayed to his phone's screen.
I tried watching some videos but neither of them solved my problem. They just show how to display it inside the app, not how to download it. Or maybe I don't know how to find that video and what keywords to use. That's why I asked here. I made my question as clear as I can.
I added these permissions in the manifest file for read and write storage, also for the internet permission- (Is there any other permission need to be added for WIFI connection?)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And I tried this code I found on the internet-
binding.downloadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(ShowImageActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(ShowImageActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ShowImageActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 123);
ActivityCompat.requestPermissions(ShowImageActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 123);
Toast.makeText(ShowImageActivity.this, "Need Permission to access storage for Downloading Image", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ShowImageActivity.this, "Downloading Image...", Toast.LENGTH_SHORT).show();
downloadImage(ShowImageActivity.this, "image",".jpg",DIRECTORY_DOWNLOADS,imageURL);
}
}
});
public void downloadImage(Context cx, String fileName, String fileExtension, String destinationDirectory, String url){
DownloadManager dm = (DownloadManager) cx.getSystemService(cx.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(cx, destinationDirectory, fileName+fileExtension);
dm.enqueue(request);
}
But it isn't working, every time I click the button the notification shows Download task failed, and nothing is being downloaded. What are the easiest ways of downloading an image or any file from an URL using Java? I asked this question before but I haven't received any answers. I hope I will get some answers this time.
You can use other libraries for the download files. I can suggest you fetch is so usefully and have a simple download listener you can try this. That library is so simple and usefull. If you still getting error you can share log with us. https://github.com/tonyofrancis/Fetch