-1

I am selecting an image from gallery and when we are clearing from recent apps the image which we selected also getting deleted.I want to display the image even though it is removed from recent apps which means I want to save the image in app.please provide me the total code.

Thanks in Advance.

Shivangi Agrawal
  • 141
  • 1
  • 10
  • You will need SQLite Database to store image path. Create database for your app first and then add image. – Onkar Nene May 11 '16 at 09:23
  • Possible duplicate of [saving image picked from gallery for future use](http://stackoverflow.com/questions/18668377/saving-image-picked-from-gallery-for-future-use) –  May 11 '16 at 09:29
  • I gave you whole code(class),accept it as answer if you are satisfied – sandesh May 11 '16 at 11:05

2 Answers2

3

package com.developerscode.com.profile_activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by android on 6/5/16.
 */
public class MainActivity extends AppCompatActivity {

    private int PICK_IMAGE_REQUEST = 1;
    ImageView image;


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

        image = (ImageView) findViewById(R.id.image);

        SharedPreferences myPrefrence = getPreferences(MODE_PRIVATE);
        String imageS = myPrefrence.getString("imagePreferance", "");
        Bitmap imageB;
        if(!imageS.equals("")) {
            imageB = decodeToBase64(imageS);
            image.setImageBitmap(imageB);
        }
    }


    public void selectImage(View v){
        Intent intent = new Intent();
// Show only images, no videos or anything else
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            InputStream stream;
            try {
                Toast.makeText(MainActivity.this, "Image saved", Toast.LENGTH_SHORT).show();
                stream = getContentResolver().openInputStream(data.getData());
                Bitmap realImage = BitmapFactory.decodeStream(stream);
               image.setImageBitmap(realImage);


                SharedPreferences myPrefrence = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor editor = myPrefrence.edit();
                editor.putString("imagePreferance", encodeToBase64(realImage));

                editor.commit();
            }
            catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

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

      Log.d("Image Log:", imageEncoded);
      return imageEncoded;
  }

    public static Bitmap decodeToBase64(String input) {
        byte[] decodedByte = Base64.decode(input, 0);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }

}
Shivangi Agrawal
  • 141
  • 1
  • 10
0

If you want your selected images to be stored even after destroying app, Why not just use SharedPreferences. Simply put your file path in Shared preferences.

Code:

     public class save  
  {

           SharedPreferences sharedPreferences;

           Context ctx;
           public save(Context ctx,String file)
                      {
                      this.ctx =ctx;
                      sharedPreferences = this.ctx.getSharedPreferences(file,Context.MODE_PRIVATE);

                      }



public void store(String key,String value)
{
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key,value);
    editor.commit();

}

public String read(String key)
{

   String v= sharedPreferences.getString(key, "nothing");
    return v;

}

public void remove()
{
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();
}
public void delete(String str){

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.remove(str);
    editor.commit();

}

public Map<String, ?> readall(){

    Map<String, ?> allEntries = sharedPreferences.getAll();

    return allEntries;
}}

To add selected path to shared preferences use method store();

to delete a path from shared preferences use method delete();

to remove all use method remove();

to read all use readall();

sandesh
  • 390
  • 6
  • 20
  • actually am using shared preferences only for saving my image. In my code i am encoding that image into base64 then saving in shared preferences and while getting from shared preferences again decoding to bitmap when using separate activity for this. Its working perfectly but when i am using it with Dashboard activity its again resetting the image to default. I dont know why? – Shivangi Agrawal May 12 '16 at 05:47
  • Sorry, I don't really understand what you exactly want to do. To reach your question to more people add proper tags, not just android. And repost it. Add tags like base64, android, gallery etc. I mean those tags should help to reach right people. Hope someone will help you – sandesh May 12 '16 at 06:14