Tuesday, September 29, 2015

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