activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8F8FF"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Image To Gallery"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivSource"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="12dp"
android:src="@drawable/flower4"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Original Image"
app:layout_constraintStart_toStartOf="@+id/ivSource"
app:layout_constraintTop_toBottomOf="@+id/ivSource" />
<ImageView
android:id="@+id/ivSaved"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="48dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvSource" />
<TextView
android:id="@+id/tvSaved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@+id/ivSaved"
app:layout_constraintTop_toBottomOf="@+id/ivSaved" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.cfsuman.androidtutorials;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
public class MainActivity extends Activity {
private ImageView mIvSaved;
private TextView mTvSaved;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
mContext = getApplicationContext();
// Get the widgets reference from XML layout
Button btn = findViewById(R.id.button);
mIvSaved = findViewById(R.id.ivSaved);
mTvSaved = findViewById(R.id.tvSaved);
btn.setOnClickListener(v -> {
// Get the image from drawable
// resource as drawable object
Drawable drawable = ContextCompat.getDrawable(
mContext,R.drawable.flower4
);
// Get the bitmap from drawable object
//assert drawable != null;
Bitmap bitmap = ((BitmapDrawable)drawable)
.getBitmap();
// Save image to gallery
String savedImageURL = MediaStore.Images.Media
.insertImage(
getContentResolver(),
bitmap,
"flower",
"Image of flower"
);
// Parse the gallery image url to uri
Uri savedImageURI = Uri.parse(savedImageURL);
// Display the saved image to ImageView
mIvSaved.setImageURI(savedImageURI);
// Display saved image url to TextView
mTvSaved.setText(
"Image saved to gallery.\n" + savedImageURL
);
});
}
}
AndroidManifest.xml [permission]
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>