Step 1:
Add Dependencies app/Gradle/build.gradle −
dependencies {
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
implementation 'com.github.CodeWithTamim:NasaDownloader:1.0.0' //add library
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Step 2:
Add Library app/Gradle/settings.gradle −
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' } //add jetpack url
}
}
Step 3:
File open res/layout/activity_main.xml file −
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/urlED"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="input image url..."
android:inputType="text" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/downlaodBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#4CAF50"
android:padding="5dp"
android:text="Download" />
</LinearLayout>
</RelativeLayout>
Step 4:
File open com.microappvalley.newtestproject/MainActivity.java file −
package com.tutorialb.imagedownloadfromurlandsaveinthefolder;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.nasahacker.downloader.NasaDownloader;
public class MainActivity extends AppCompatActivity {
EditText urlED;
AppCompatButton downlaodBTN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
urlED = findViewById(R.id.urlED);
downlaodBTN = findViewById(R.id.downlaodBTN);
NasaDownloader downloader = new NasaDownloader("SavingDirName");
downlaodBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String imageURL = urlED.getText().toString();
if (!downloader.isPermissionGranted(MainActivity.this)) {
downloader.requestPermission(MainActivity.this);
} else {
downloader.downloadImage(MainActivity.this, imageURL, NasaDownloader.IMAGE_TYPE.JPEG);
}
}
});
}
}