Luke Villanueva
Luke Villanueva

Reputation: 2070

Save image to phone loaded from gallery

I'm trying to save an image loaded from the gallery to the phone memory(local path). Can anyone guide me into this?

This is how I get the image from the gallery.

ImageView profilePicture;
private Uri imageUri;
String picturePath;

@Override
public void onCreate(Bundle savedInstanceState)  
{
     profilePicture = (ImageView) findViewById(R.id.profile_picture);
        profilePicture.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
                case MotionEvent.ACTION_UP:{
                    uploadImage();
                    break;
                }
                }
                return true;
            }
        });
}

uploadImage()

Intent galleryIntent = new Intent(Intent.ACTION_PICK,     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
startActivityForResult(galleryIntent, 1);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 0:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);

                     profilePicture.setImageBitmap(bitmap);
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }
        case 1:
             if (resultCode == Activity.RESULT_OK && null != data) {
                 Uri selectedImage = data.getData();
                 String[] filePathColumn = { MediaStore.Images.Media.DATA };

                 Cursor cursor = getContentResolver().query(selectedImage,
                         filePathColumn, null, null, null);
                 cursor.moveToFirst();

                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                 picturePath = cursor.getString(columnIndex);
                 cursor.close();
                 profilePicture.setBackgroundColor(Color.TRANSPARENT);
                     profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath));

             }

        }
    }

*Note: Case 0 is for image capturing using phones camera.

I can display it on my imageview but I need to store this in the phone's memory so everytime I will open the app, I will be able to load the previous uploaded image to the image view. Then if the user wants to upload again. The file previously saved will just be overwritten. I don't want to result to storing images as blob using sqlite since I will be uploading just one image for my whole app. I want to store it in a local file path like myappname/images/image.png. Any ideas? Thanks!

Upvotes: 0

Views: 522

Answers (1)

Marcin S.
Marcin S.

Reputation: 11191

You can store an image in the application cache directory such as:

try {
    String destFolder = getCacheDir().getAbsolutePath()+ "/images/";
        if (!new File(destFolder).exists()) {
            new File(destFolder).mkdirs();
        }

        FileOutputStream out = new FileOutputStream(destFolder + "profile.png");    
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
} catch (Exception ex) {
    ex.printStackTrace();               
}

And read back the file into the Bitamp:

String fname = "profile.png";
Bitmap profile = BitmapFactory.decodeFile(getCacheDir().getAbsolutePath()+ "/images/"  + fname);

Upvotes: 1

Related Questions