0

In my webservice i have simple upload function which tested by cUrl and its work fine without any problem, and I can upload file by cUrl successfully, now I want to upload file from android with Ion library, but i get this error:

unable to parse json

my code is:

Ion.with(ActivityAccountInfo.this)
                .load(MyApplication.getHostAddress() + "clientUploadPhoto")
                .uploadProgressHandler(new ProgressCallback() {
                    @Override
                    public void onProgress(long downloaded, long total) {
                        uploadMessage.setText("" + downloaded + " / " + total);
                    }
                })
                .setTimeout(60 * 60 * 1000)
                .setMultipartFile("userPhoto", "image/*", new File(photoPath))
                .asJsonObject()
                // run a callback on completion
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        if (e != null) {

                        }
                    }
                });

and my web function:

Route::any('clientUploadPhoto', function () {
    $filename = Request()->file('userPhoto');
    $destinationPath = base_path() . '/upload/';
    $new_filename = time() . '_' . $filename->getClientOriginalName();
    Image::make($filename->getRealPath())->save($destinationPath . $new_filename);
});
Alonso Urbano
  • 2,266
  • 1
  • 19
  • 26
DolDurma
  • 15,753
  • 51
  • 198
  • 377

1 Answers1

0

Problem resolved by other solution, after set bitmap into ImageView i get bitmap from that with this code:

Bitmap bitmap = ((BitmapDrawable) user_avatar.getDrawable()).getBitmap();

and after that i'm create Base64 from that:

public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);
    byte[] imageBytes   = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

now i can upload that with Ion and i convert that inside server:

php Laravel framework:

public function clientUploadPhoto()
{
    $image = Request::json()->get('file');
    $ext = Request::json()->get('ext');

    file_put_contents( base_path() . '/upload/'.time().$ext,base64_decode($image));
}
DolDurma
  • 15,753
  • 51
  • 198
  • 377