-1

Can I know what the method to

  1. Open a file manager via Intent when a button is clicked
  2. Select and display that Image in ImageView
  3. Use that Image so that it can be converted to Base64

Thank You

Sadir Omer
  • 48
  • 1
  • 9
  • Refer to http://javatechig.com/android/writing-image-picker-using-intent-in-android – Ophitect Jan 27 '16 at 03:28
  • Will do Thank You. I can get to the part where i can display the image in ImageView. After that to use the image should i use "imageUri" from the code? – Sadir Omer Jan 27 '16 at 03:35
  • Use the bitmap that was created by decoding the image stream. Convert the bitmap to base64 encoding. There is a article here: http://stackoverflow.com/questions/9768611/encode-and-decode-bitmap-object-in-base64-string-in-android – Ophitect Jan 27 '16 at 03:37

1 Answers1

4

Pick image from gallery

public void pickImage() {
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("image/*");
      startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
            if (data == null) {
                //Display an error
                return;
            }
            InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
            //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
        }
    }

Encode and Decode image into Base64

public static String encodeTobase64(Bitmap image)
{
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}
public static Bitmap decodeBase64(String input) 
{
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
}
Ahsan Kamal
  • 1,085
  • 2
  • 13
  • 34