Wednesday , June 29 2022

Retrofit Upload File Tutorial – Uploading and Downloading Images – Android Coding by DMTechnolab

The package The trap.Simplify.Retrophytylphilode;

Import Android.Clearly;

Import Android.Content.Intent;

Import the evening.package manager;

Import Android.The database.Cursor;

Import Android.The hate;

Import Android.The.the construction of;

Import Android.the provider.Mediastore;

Import Android.the provider.Adjustment;


Import Android.Support.v4.Content.CursorLoader;

Import Android.Support.v7.App.AppCompatActivity;

Import Android.The.Bundle;

Import Android.Opinion.Opinion;

Import Android.Widget.Toast;

Import With.Google.the resurrection.Arises;

Import With.Google.the resurrection.GsonBuilder;

Import Java.I am.The file;

Import Java.I am.IOException;

Import type;

Import okhttp3.Request;

Import okhttp3.Feedback;

Import Retrofit 2.Call out;

Import Retrofit back;

Import Retrofit 2.Feedback;

Import Retrofit 2.move back;

Import Retrofit 2.Converter.the resurrection.GsonConverterFactory;

public class Main activity Is spread AppCompatActivity {{

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


// checking permission

If ()the construction of.the version.SDK_INT > = the construction of.VERSION_CODES.M &&,


! = package manager.permission granted) {{

Intent is intended = New Intent()Adjustment.ACTION_APPLICATION_DETAILS_SETTINGS,

to hate.Parse()“Package:” + getPackageName())));


Beginning()is intended);

The return;


// add the click listener to the button

findViewById()R.Id.switch).setOnClickListener()New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion Opinion) {{

// file open selector

Intent I am = New Intent()Intent.ACTION_PICK, Mediastore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult()I am, 100);




@ Override

Protected Zero onActivityResult()int request code, int results, Intent The data) {{

Super.onActivityResult()request code, results, The data);

If ()request code == 100 && results == RESULT_OK && The data ! = Invalid) {{

// image URI

to hate Selected = The data.get data());

// calling the upload file method after selecting the file

Upload document()Selected, “my image”);



Private Zero Upload documents()to hate fileUri, String Descend) {{

// Creating a file

The file The file = New The file()getRealPathFromURI()fileUri));

// Creating request body for file

request request = request.To create()media type.Parse()getContentResolver()).getType()fileUri)), The file);

Request Descent = request.To create()media type.Parse()“Text / plain”), Descend);

// Gone Builder

Arises the resurrection = New GsonBuilder())


.To create());

// Creating a retrofit object

move back move back = New move back.Builder())


.addConverterFactory()GsonConverterFactory.To create()the resurrection))


// create our API

Your Your = move back.To create()Your.class);

// make call call upload image method

Call out call = Your.upload image()request, Descent);

// finally call

call.Begrudge()New call back()) {{

@ Override

public Zero Treason()Call out call, Feedback Feedback) {{

If ()!Feedback.Body()).Error) {{

Toast.Makeup()getApplicationContext()), “File uploaded successfully …”, Toast.LENGTH_LONG).Display());

} other {{

Toast.Makeup()getApplicationContext()), “Something went wrong …”, Toast.LENGTH_LONG).Display());



@ Override

public Zero Failure()Call out call, Throwable T) {{

Toast.Makeup()getApplicationContext()), T.getMessage()), Toast.LENGTH_LONG).Display());




/ *

* This method returns the full path to the image file

* If you want to upload other kind of files like .pdf, .docx

* You only need to change this method

* The rest will be the same

* * /

Private String getRealPathFromURI()to hate Content) {{

String[] Prose = {{Mediastore.Images.Media.The data};

CursorLoader Loader = New CursorLoader()this, Content, Prose, Invalid, Invalid, Invalid);

Cursor Cursor = Loader.loadInBackground());

int column_index = Cursor.getColumnIndexOrThrow()Mediastore.Images.Media.The data);


String results = Cursor.Get up()column_index);


The return results;



Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *