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

时间:2016-05-11 09:14:11

标签: android

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

先谢谢。

2 个答案:

答案 0 :(得分:3)

  

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

}

答案 1 :(得分:0)

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

代码:

       private void button1_Click(object sender, EventArgs e)
    {
        using (var client = new WebClient())
        {
            var json = client.DownloadString("http://private-anon-397aeee05-karhoofleetintegration.apiary-mock.com/trip?version=2&karhoo_ref=karhoo_ref.asmx");
            var serializer = new JavaScriptSerializer();
             BookingInformation bo = serializer.Deserialize<BookingInformation>(json.ToString());

        }
    }


public class BookingInformation
{
      public string karhoo_ref { get; set; }
public string booking_id { get; set; }


public string datetime_scheduled_utc { get; set; }
public string status { get; set; }
public bool is_asap { get; set; }

public decimal latitude { get; set; }

public decimal longitude { get; set; }


}

要将选定路径添加到共享首选项,请使用method store();

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

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

阅读全部使用readall();