TrinityTuts

Upload image to server from internal or external memory android

Last updated on January 9th, 2020 at 12:33 pm

In some of my previous post  i explain how to upload capture image to server. But now there are some issue i also face, uploading the image to server mainly with original image.  In simple words when we try to upload image we get from onActivityResult. In this post i did not explain how we can capture image and how we can upload data to server i already create post for these thing please read. In this post i explain how to get original path of image from intent data you get in onActivityResult as shown bellow.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
   super.onActivityResult(requestCode, resultCode, data);
      Intent sectedImage = data.getData();
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      // ... Set this bitmap in imagview to get preview
}

Now to upload your original image store in your device first we need to get original path of image from storage and after that we convert that image to byte and than Base64 and upload it to server.

Now i create a method to get real path of image from temp Uri

public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int id = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(id);
    }

Now i call this method to get original image from internal storage.

Upload image to server from internal or external memory android

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)     {
   super.onActivityResult(requestCode, resultCode, data);
      Uri selectedImage = data.getData();
      Bitmap photoToBase64 = BitmapFactory.decodeFile(getRealPathFromURI(selectedImage));
      ByteArrayOutputStream bao;
      bao = new ByteArrayOutputStream();
      photoToBase64.compress(Bitmap.CompressFormat.JPEG, 90, bao);
      byte[] ba = bao.toByteArray();
      ba1 = Base64.encodeBytes(ba);

      // Upload your encoded image to server
      
 }

public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int id = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(id);
    }

Now in above code i simply get my original image path from intent data and then i convert my image in bytes and after that i call bitmap and do some operation needed and then convert image to base64.

Please use this class to encode your image in base64. And also add entry in application for heap memory to resolve memory issue.

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme">

</application>

And in server side you need to decode the code. and convert that string to image.

<?php
error_reporting(E_ALL);
if(isset($_POST['ImageName'])){
$imgname = $_POST['ImageName'];
$imsrc = base64_decode($_POST['base64']);
$fp = fopen($imgname, 'w');
fwrite($fp, $imsrc);
if(fclose($fp)){
	echo "Image uploaded";
}else{
	echo "Error uploading image";
}
}
?>

Hope this will help you to get your original image :-p . For any query please let me know in by posting comment.

Happy coading 🙂