0

I want to allow user to upload image from gallery into an android application that i am creating. At the moment i am able to allow the user to chose the image from the gallery here in the codes below:

/* Choose an image from Gallery */
void openImageChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}

Then onActivityResult i try to get the path of the image and convert it into URI then i later convert it into into Bitmap to display to the user in my codes below:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_IMAGE) {
            // Get the url from data
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                // Get the path from the Uri
                String path = getPathFromURI(selectedImageUri);
                Log.i(TAG, "Image Path : " + path);
                // Set the image in ImageView
                profileImage.setImageURI(selectedImageUri);
            }
            final InputStream imageStream;
            try {
                assert selectedImageUri != null;
                imageStream = getContentResolver().openInputStream(selectedImageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                encodedImage = encodeImage(selectedImage);
                Log.i("encodedImage", encodedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
}

public String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

private String encodeImage(Bitmap bm)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String encImage = Base64.encodeToString(b, Base64.DEFAULT);

    return encImage;
}

On click of this button below i want to save the encoded image into SharedPreferences so when the user starts the application again i can show that image to the user but unfortunately i am unable to get the encoded image and i don't know how to set it on onCreateView method.

btnAddImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("image", encodedImage);
            editor.apply();
            showSnackSuccess();
        }
    });
Lending Square
  • 201
  • 1
  • 5
  • 14
  • 1
    "i try to get the path of the image" -- there is no "path of the image". There is no requirement for `ACTION_GET_CONTENT` to return a `Uri` for which your broken `getPathFromURI()` will work. Moreover, you do not need it. You are already getting a `Bitmap` using `openInputStream()`. Just use that with the `ImageView` (and, eventually, move all of that I/O off the main application thread). In terms of saving the image, get rid of the base64 encoding, and simply save the image bytes to a `.png` file (e.g., in `getFilesDir()`). – CommonsWare Sep 02 '17 at 15:20
  • @CommonsWare can you show me how to save the image bytes to a .png file? – Lending Square Sep 02 '17 at 18:14
  • Instead of using `imageStream` to create a `Bitmap`, use `imageStream`, a `FileOutputStream` on your desired file, and standard Java I/O to copy the bytes from `inputStream` to the `FileOutputStream`. – CommonsWare Sep 02 '17 at 18:16
  • @CommonsWare is very confusing..pardon my english but i can't seem to understand – Lending Square Sep 02 '17 at 21:43
  • Um, Java I/O is covered in any decent book on Java. A search for `java copy input stream to outputstream` on a search engine will turn up answers like [this one](https://stackoverflow.com/a/40019374/115145). – CommonsWare Sep 02 '17 at 21:51

0 Answers0