2016-05-11 51 views
-2

我从图库中选择图像,当我们从最近的应用程序中清除时,我们选择的图像也被删除。即使图像被删除,我也想显示该图像从最近的应用程序,这意味着我想保存图像在app.please提供我的总代码。从图库中选择图像并将其保存在Android应用程序中

在此先感谢。

+0

您将需要SQLite数据库来存储图像路径。首先为您的应用程序创建数据库,然后添加图像。 –

+0

[保存从图库中挑选的图像以备将来使用]的可能的重复(http://stackoverflow.com/questions/18668377/saving-image-picked-from-gallery-for-future-use) –

+0

我给了你整个代码(类),如果您满意,请将其作为答案接受 – sandesh

回答

0

如果您希望即使在销毁应用程序之后仍然可以存储选定的图像,为什么不使用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; 
}} 

要共享偏好,使用方法存储(添加所选路径);

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

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

要全部读取使用readall();

+0

实际上我仅使用共享偏好来保存图像。在我的代码中,我将该图像编码到base64中,然后保存在共享首选项中,同时从共享首选项获取解码到位图时使用单独的活动。它的工作完美,但是当我使用它与仪表板活动时,它再次将图像重置为默认。我不知道为什么? –

+0

谢谢@sandesh,但请检查我的上面的代码 –

+0

对不起,我真的不明白你到底想做什么。为了让更多人了解您的问题,请添加适当的标签,而不仅仅是android。并重新发布。添加像base64,android,画廊等标签。我的意思是这些标签应该有助于达到正确的人。希望有人会帮助你 – sandesh

1

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

} 
相关问题