skip to Main Content

How do I let a user save an image from gallery to his SharedPreferences?

I am now using this code so the user can select an image from his preferred gallery. How do i save his picture of choice to the SharedPreferences?

if (v.getId() == R.id.btnUploadPicture)
{
    Intent uploadPic = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    final int ACTIVITY_SELECT_IMAGE = 1234;
    startActivityForResult(uploadPic, ACTIVITY_SELECT_IMAGE);
}

2

Answers


  1. You can save the image path in the shared preferences and later get the image path from the shared preferences.

    The image path can be obtained like this.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
    
            Uri tempUri = getImageUri(getApplicationContext(), photo);
    
            File finalFile = new File(getRealPathFromURI(tempUri));
    
            System.out.println(mImageCaptureUri);
        }  
    }
    
    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }
    
    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        return cursor.getString(idx); 
    }
    
    Login or Signup to reply.
  2. Correct me if I’m wrong. But it sounds looks like the real issue is that you don’t know how to handle the image when you return from the startActivityForResult.
    When you use startActivityForResult, onActivityResult is always ran when it returns from said activity.
    You’ll need to override onActivityResult in your class.

    I suggesting making

    final int ACTIVITY_SELECT_IMAGE = 1234;
    

    a global variable so you can use it in startActivityForResult. Because you need to surround your code with an if statement, so that it doesn’t get ran when returning from another activity.

    Place the following override method in your class.

     @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
     if (requestCode == ACTIVITY_SELECT_IMAGE) {
        if (resultCode == Activity.RESULT_OK) {
            System.out.println("Successfully Returned from Gallery with Image")
            //This is your image from the gallery
            if (data != null) {
                Uri fileUri = data.getData();
                //This is where you will do what you need with the image. 
                //Maybe set an ImageView
                //imageView.setImageURI(fileUri);
                //Or you can copy the image to the app's gallery
                //Add URI to SharedPrefs 
    
            }
    
        } else if (resultCode == Activity.RESULT_ERROR) {
            Toast.makeText(this, "An Error Has Occurred", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show();
        }
     }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search