简体   繁体   中英

How to save the gallery image Android?

I want to show the user image in my application, so i took a image view in navigation drawer.

First i get the image from gallery and set that image to Image view. But whenever i close the app and open the image was not showing (what i selected from gallery ). So i want to save the image in internal memory.

I tried but the image is not showing.

Please any one help me.

Thank you in advance.

My code:

  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;

        }
  1. Maintain a flag for user_image use sharedPref.
  2. If set then set user_image from stored file to ImageView
  3. Else load some default profile_image

try on this way.

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.The Directory will be created with the given name. Javadocs is for to tell where exactly it will create the directory.

2.You will have to give the image name by which you want to save it.

and load it's save image like this way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM