android - How to save image to file in external storage






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 External Storage"
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/flower3"
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.os.Environment;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


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.flower3
);

// Get the bitmap from drawable object
//assert drawable != null;
Bitmap bitmap = ((BitmapDrawable) drawable)
.getBitmap();

// Initializing a new file
File file;

// Get the external storage directory path
String path = Environment.getExternalStorageDirectory()
.toString();

// Create a file to save the image
file = new File(path, "UniqueFileName" + ".jpg");

try {
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(
Bitmap.CompressFormat.JPEG,
100, stream
);
stream.flush();
stream.close();

} catch (IOException e) // Catch the exception
{
e.printStackTrace();
}

// Parse the saved image path to uri
Uri savedImageURI = Uri.parse(file.getAbsolutePath());

// Display the saved image to ImageView
mIvSaved.setImageURI(savedImageURI);

// Display saved image uri to TextView
mTvSaved.setText(
"Image saved in external storage.\n"
+ savedImageURI
);
});
}
}



AndroidManifest.xml [permission]



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>








android - How to save an image to gallery






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"/>








android - How to take ScreenShot programmatically






activity_main.xml



<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="@+id/rootLayout"
tools:context=".MainActivity"
android:background="#DCDCDC">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take ScreenShot"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:src="@drawable/flower7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>





MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.app.Activity;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.constraintlayout.widget.ConstraintLayout;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);
ConstraintLayout rootLayout = findViewById(R.id.rootLayout);

button.setOnClickListener(view -> {
// Take the screenshot
Bitmap screenShot = TakeScreenShot(rootLayout);

// Save the screenshot on device gallery
MediaStore.Images.Media.insertImage(
getContentResolver(),
screenShot,
"Image",
"Captured ScreenShot"
);

// Notify the user that screenshot taken.
Toast.makeText(
getApplicationContext(),
"Screen Captured.",
Toast.LENGTH_SHORT
).show();
});
}


// Custom method to take screenshot
public Bitmap TakeScreenShot(View rootView){
// Screenshot taken for the specified root
// view and its child elements.
Bitmap bitmap = Bitmap.createBitmap(
rootView.getWidth(),
rootView.getHeight(),
Bitmap.Config.ARGB_8888
);
Canvas canvas = new Canvas(bitmap);
rootView.draw(canvas);
return bitmap;
}
}



AndroidManifest.xml [permission]



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>









android - How to change ActionBar title programmatically








MainActivity.java



package com.cfsuman.androidtutorials;

import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button button = findViewById(R.id.button);


// Set the button click listener
button.setOnClickListener(view -> {
// Get the ActionBar
ActionBar actionBar = getSupportActionBar();

// Change the ActionBar title text
assert actionBar != null;
actionBar.setTitle("Updated ActionBar Title");
});
}
}





activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change ActionBar Title Text"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>