Skip to content

A simple library for downloading or saving images from a URL or bitmap in Android.

License

Notifications You must be signed in to change notification settings

CodeWithTamim/NasaDownloader

Repository files navigation

Nasa Downloader

Latest Version GitHub Stars

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.

Features

  • Download images from URLs
  • Save images from bitmaps
  • Supports multiple image formats (JPEG, PNG, etc.)
  • Manages permissions for external storage access

Installation

To use this library, add the following dependency to your build.gradle or build.gradle.kts file. The library is hosted on JitPack.

Groovy

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.CodeWithTamim:NasaDownloader:1.0.6'
}

Kotlin

allprojects {
    repositories {
        ...
        maven { url = uri("https://jitpack.io") }
    }
}

dependencies {
    implementation("com.github.CodeWithTamim:NasaDownloader:1.0.6")
}

Usage

Initialization

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
)

Download Image from URL

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.

Java

 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
    }

Kotlin

lifecycleScope.launch(Dispatchers.IO) {
nasaDownloader.downloadImageFromUrl(
    url = "https://example.com/image.jpg",
    format = ImageFormat.JPEG,
    onSuccess = { file ->
        // Handle success
    },
    onFailure = { exception ->
        // Handle failure
    }
)
}

Save Bitmap

To save a bitmap image:

Java

 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
    }

Kotlin

lifecycleScope.launch(Dispatchers.IO) {
nasaDownloader.saveBitmap(
    bitmap = myBitmap,
    format = ImageFormat.PNG,
    onSuccess = { file ->
        // Handle success
    },
    onFailure = { exception ->
        // Handle failure
    }
)
}

Permissions

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)
}

License

This project is licensed under the Apache 2.0 License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request here.

Contact

For any inquiries, please contact tamimh.dev@gmail.com.


Made with ❤️ by Tamim Hossain

About

A simple library for downloading or saving images from a URL or bitmap in Android.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages