繁体   English   中英

从图库中选择图像并将其保存在Android App中

[英]Selecting an Image from gallery and to save it in Android App

我正在从图库中选择一个图像,当我们从最近的应用程序中清除选择的图像时,该图像也会被删除。即使它已从最近的应用程序中删除,我仍要显示该图像,这意味着我要将图像保存在app.please向我提供总代码。

提前致谢。

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

}

如果您要在销毁应用程序后仍存储所选图像,为什么不使用SharedPreferences。 只需将您的文件路径放在“共享”首选项中。

码:

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

要将选择的路径添加到共享首选项,请使用方法store();。

要从共享首选项中删除路径,请使用delete()方法;

删除所有使用方法remove();

阅读所有使用readall();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM