[英]Selecting an Image from gallery and to save it in Android App
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. 我正在从图库中选择一个图像,当我们从最近的应用程序中清除选择的图像时,该图像也会被删除。即使它已从最近的应用程序中删除,我仍要显示该图像,这意味着我要将图像保存在app.please向我提供总代码。
Thanks in Advance. 提前致谢。
package com.developerscode.com.profile_activity;
包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);
}
}
If you want your selected images to be stored even after destroying app, Why not just use SharedPreferences. 如果您要在销毁应用程序后仍存储所选图像,为什么不使用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(); 要将选择的路径添加到共享首选项,请使用方法store();。
to delete a path from shared preferences use method delete(); 要从共享首选项中删除路径,请使用delete()方法;
to remove all use method remove(); 删除所有使用方法remove();
to read all use readall(); 阅读所有使用readall();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.