Nasa Downloader is a library designed to download images from URLs or bitmaps and save them on an Android device. It supports saving images in various formats and manages permissions for accessing external storage.
- Download images from URLs
- Save images from bitmaps
- Supports multiple image formats (JPEG, PNG, etc.)
- Manages permissions for external storage access
To use this library, add the following dependency to your build.gradle
or build.gradle.kts
file. The library is hosted on JitPack.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.CodeWithTamim:NasaDownloader:1.0.6'
}
allprojects {
repositories {
...
maven { url = uri("https://jitpack.io") }
}
}
dependencies {
implementation("com.github.CodeWithTamim:NasaDownloader:1.0.6")
}
To get the singleton instance of NasaDownloader
, use the following code:
val nasaDownloader = NasaDownloader.getInstance(
saveDirName = "MyImages", // Optional, default is "NasaDownloader"
imageQuality = 80 // Optional, default is 100
)
To download an image from a URL and save it:
Note : After downloading the image it saves to the specified directory, don't think that you need to save it manually as the callback returns a file.
ExecutorService executorService = Executors.newSingleThreadExecutor(); // Create a single-threaded executor
executorService.submit(new Runnable() {
@Override
public void run() {
try {
nasaDownloader.downloadImageFromUrl(
"https://example.com/image.jpg",
ImageFormat.JPEG,
new DownloadCallback() {
@Override
public void onSuccess(File file) {
// Handle success
}
@Override
public void onFailure(Exception exception) {
// Handle failure
}
}
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executorService.shutdown(); // Shut down the executor after task submission
}
lifecycleScope.launch(Dispatchers.IO) {
nasaDownloader.downloadImageFromUrl(
url = "https://example.com/image.jpg",
format = ImageFormat.JPEG,
onSuccess = { file ->
// Handle success
},
onFailure = { exception ->
// Handle failure
}
)
}
To save a bitmap image:
ExecutorService executorService = Executors.newSingleThreadExecutor(); // Create a single-threaded executor
executorService.submit(new Runnable() {
@Override
public void run() {
try {
nasaDownloader.saveBitmap(
myBitmap,
ImageFormat.PNG,
new DownloadCallback() {
@Override
public void onSuccess(File file) {
// Handle success
}
@Override
public void onFailure(Exception exception) {
// Handle failure
}
}
);
} catch (Exception e) {
e.printStackTrace();
}
}
});
executorService.shutdown(); // Shut down the executor after task submission
}
lifecycleScope.launch(Dispatchers.IO) {
nasaDownloader.saveBitmap(
bitmap = myBitmap,
format = ImageFormat.PNG,
onSuccess = { file ->
// Handle success
},
onFailure = { exception ->
// Handle failure
}
)
}
Add the following permissions to your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Check if the necessary permissions are granted:
if (!nasaDownloader.isPermissionsGranted(activity)) {
nasaDownloader.requestPermissions(activity)
}
This project is licensed under the Apache 2.0 License.
Contributions are welcome! Please open an issue or submit a pull request here.
For any inquiries, please contact tamimh.dev@gmail.com.
Made with ❤️ by Tamim Hossain