如何保存图库Android?

时间:2016-01-08 08:28:43

标签: android imageview android-gallery internal-storage

我想在我的应用程序中显示用户图像,所以我在导航抽屉中拍摄了一个图像视图。

首先我从图库中获取图像并将该图像设置为图像视图。但每当我关闭应用程序并打开图像时都没有显示(我从图库中选择了什么)。所以我想将图像保存在内存中。

我试过但图像没有显示。

请任何人帮助我。

提前谢谢。

我的代码:

  rightimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

    //gallery
                    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, RESULT_LOAD_IMAGE);

                }
            });

     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RESULT_LOAD_IMAGE  && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                rightimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    //saving image in internal storage.
                saveImageToInternalStorage(BitmapFactory.decodeFile(picturePath));
            }
        }
    // //saving image in internal storage.

        public boolean saveImageToInternalStorage(Bitmap image) {

            try {
    // Use the compress method on the Bitmap object to write image to
    // the OutputStream
                 fos = getActivity().openFileOutput(file, Context.MODE_PRIVATE);

    // Writing the bitmap to the output stream
                image.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();

                return true;
            } catch (Exception e) {
                Log.e("saveToInternalStorage()", e.getMessage());
                return false;
            }
        }


        public Bitmap getThumbnail(String filename) {


            Bitmap thumbnail = null;



    // If no file on external storage, look in internal storage

                try {
                    Log.v("RTAG_IMAG",""+"IMG");
                    File filePath = getActivity().getFileStreamPath(filename);
                    FileInputStream fi = new FileInputStream(filePath);
                    thumbnail = BitmapFactory.decodeStream(fi);
                    rightimage.setImageBitmap(thumbnail);
                } catch (Exception ex) {
                    Log.e("getThumbnail()", ex.getMessage());
                }

            return thumbnail;

        }

2 个答案:

答案 0 :(得分:0)

  1. 使用sharedPref维护user_image的标志。
  2. 如果设置,则将user_image从存储文件设置为ImageView
  3. 否则加载一些默认的profile_image

答案 1 :(得分:0)

尝试这种方式。

private String saveToInternalSorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
   // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
          fos.close(); 
    } 
    return directory.getAbsolutePath();
}

} 1.将使用给定名称创建目录。 Javadocs用于告诉它将在何处创建目录。

2.您必须提供要保存的图像名称。

并像这样加载它的保存图像。

private void loadImageFromStorage(String path)

{

try {
    File f=new File(path, "profile.jpg");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(R.id.imgPicker);
    img.setImageBitmap(b);
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}