2016-05-11 54 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); 
    } 

} 
相關問題