Thursday, 31 May 2018

Load Image from gallery

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bitmap = null;
    String selectedImagePath = null;
 
   if (resultCode == RESULT_OK && requestCode == GALLERY_PICTURE) {
      if (data != null) {
        Uri selectedImage = data.getData();
        selectedImagePath = getPath(selectedImage);
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(filePath[0]);
        selectedImagePath = c.getString(columnIndex);
        c.close();
        if (selectedImagePath != null) {
            Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object            byte[] byteArrayImage = baos.toByteArray();
            String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
            byte[] decodeResponse = Base64.decode(encodedImage, Base64.DEFAULT | Base64.NO_WRAP);
            bitmap = BitmapFactory.decodeByteArray(decodeResponse, 0, decodeResponse.length); // load            bitmap = Bitmap.createScaledBitmap(bitmap, 400, 400, false);
            iv_profile.setImageBitmap(bitmap);
            imageUpload(selectedImagePath);
        }
    } 
}

No comments:

Post a Comment