Main Activity.java
package com.example.imag_save_shareprefearnce;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView textEncode;
private String encodedImage;
private ImageView imageView1;
private ProgressDialog pDialog;
private ProgressBar pb;
private int progress;
private Editor editor;
private Bitmap bm;
private SharedPreferences preferences;
private String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textEncode = (TextView) findViewById(R.id.txt);
imageView1 = (ImageView) findViewById(R.id.img);
pb = (ProgressBar) findViewById(R.id.progressBar1);
preferences = getSharedPreferences("Pref", MODE_PRIVATE);
editor = preferences.edit();
str = preferences.getString("image_data", null);
if (str == null) {
new LoadImage().execute("http://www.learn2crack.com/wp-content/uploads/2014/04/node-cover-720x340.png");
} else {
imageView1.setImageBitmap(decodeBase64(str));
}
}
// ======encodeTobase64 to string===================================
public static String encodeTobase64(Bitmap image) {
Bitmap immage = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
// ======decodeBase64 to bitmap===================================
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
// =================== Load imageAsyncTask****************
private class LoadImage extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(MainActivity.this);
// pDialog.setMessage("Loading Image ....");
// pDialog.show();
progress = 0;
pb.setVisibility(View.VISIBLE);
textEncode.setVisibility(View.VISIBLE);
}
protected Bitmap doInBackground(String... args) {
try {
bm = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
while (progress < 100) {
progress += 1;
publishProgress(progress);
SystemClock.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
protected void onProgressUpdate(Integer... values) {
/*--- show download progress on main UI thread---*/
pb.setProgress(values[0]);
textEncode.setText(values[0] + "%");
super.onProgressUpdate(values);
}
protected void onPostExecute(Bitmap image) {
if (image != null) {
encodedImage = encodeTobase64(image);
editor.putString("image_data", encodedImage);
editor.commit();
String str = preferences.getString("image_data", null);
imageView1.setImageBitmap(decodeBase64(str));
// pDialog.dismiss();
} else {
// pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
}
package com.example.imag_save_shareprefearnce;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView textEncode;
private String encodedImage;
private ImageView imageView1;
private ProgressDialog pDialog;
private ProgressBar pb;
private int progress;
private Editor editor;
private Bitmap bm;
private SharedPreferences preferences;
private String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textEncode = (TextView) findViewById(R.id.txt);
imageView1 = (ImageView) findViewById(R.id.img);
pb = (ProgressBar) findViewById(R.id.progressBar1);
preferences = getSharedPreferences("Pref", MODE_PRIVATE);
editor = preferences.edit();
str = preferences.getString("image_data", null);
if (str == null) {
new LoadImage().execute("http://www.learn2crack.com/wp-content/uploads/2014/04/node-cover-720x340.png");
} else {
imageView1.setImageBitmap(decodeBase64(str));
}
}
// ======encodeTobase64 to string===================================
public static String encodeTobase64(Bitmap image) {
Bitmap immage = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
// ======decodeBase64 to bitmap===================================
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
// =================== Load imageAsyncTask****************
private class LoadImage extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// pDialog = new ProgressDialog(MainActivity.this);
// pDialog.setMessage("Loading Image ....");
// pDialog.show();
progress = 0;
pb.setVisibility(View.VISIBLE);
textEncode.setVisibility(View.VISIBLE);
}
protected Bitmap doInBackground(String... args) {
try {
bm = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
while (progress < 100) {
progress += 1;
publishProgress(progress);
SystemClock.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
protected void onProgressUpdate(Integer... values) {
/*--- show download progress on main UI thread---*/
pb.setProgress(values[0]);
textEncode.setText(values[0] + "%");
super.onProgressUpdate(values);
}
protected void onPostExecute(Bitmap image) {
if (image != null) {
encodedImage = encodeTobase64(image);
editor.putString("image_data", encodedImage);
editor.commit();
String str = preferences.getString("image_data", null);
imageView1.setImageBitmap(decodeBase64(str));
// pDialog.dismiss();
} else {
// pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
}
main Xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="0" />
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Mainfest entry:
<uses-permission android:name="android.permission.INTERNET"/>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="0" />
</LinearLayout>
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Mainfest entry:
<uses-permission android:name="android.permission.INTERNET"/>