How to store drawable JPEG, Bitmap, PNG image file to external memory card storage space programmaticlly.
In this tutorial we are simply saving the the drawable resource image which already saves into drawable folder inside android application to external storage. We are storing image on button click in memory card of our device. So here is the complete step by step tutorial for Save/Store image to External storage android example tutorial.
Note: Put the sample image into drawable-hdpi folder.
This is the demo image.
Note : Please add WRITE_EXTERNAL_STORAGE to your AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How to Save/Store image to External storage android example tutorial.
Code for MainActivity.java file.
package com.example.storeimageexternalstorage_android_examples.com; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { Button button; ImageView imageview; Bitmap bitmap; View view; ByteArrayOutputStream bytearrayoutputstream; File file; FileOutputStream fileoutputstream; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button1); imageview = (ImageView)findViewById(R.id.imageView1); bytearrayoutputstream = new ByteArrayOutputStream(); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Drawable drawable = getResources().getDrawable(R.drawable.demo_img); Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap(); bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream); file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png"); try { file.createNewFile(); fileoutputstream = new FileOutputStream(file); fileoutputstream.write(bytearrayoutputstream.toByteArray()); fileoutputstream.close(); } catch (Exception e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show(); } }); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.storeimagetointernalstorage_android_examples.com.MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/demo_img" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:layout_marginTop="23dp" android:text="Click Here to Store image to External storage in android" /> </RelativeLayout>
Code for AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.storeimageexternalstorage_android_examples.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Screenshot: