I am new in android ,and am trying to upload a file to server using ion library ion library but the file crashes. with this error.
FATAL EXCEPTION: Thread-5572
Well i have found out a simple way of uploading or posting multipart/form-data with an upload progress bar. may be this could help.
Suppose you server is waiting for the following data :
1.File(PDF,Image,Audio,Video etc.).
2.Name
3.Email
4.Address.
and you want to upload that information once in a URL be it an Absolute URL or a Relative URL, ion library is easy , precise, efficient and much better in this.
STEP 1:
declare your URI
private Uri filePath;
STEP 2:
then Pick the file from your gallery , here it's your choice you identify any kind of files you want a user to view, me here i have identified a PDF file.
on this line intent.setType("application/pdf");
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PICK_PDF_REQUEST);
}
//handling the image chooser activity result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
Toast.makeText(getApplicationContext(), "" + filePath, Toast.LENGTH_LONG).show();
}
}
then you can call showFileChooser()
in the Button
for a click to access your gallery.
STEP 3:
a) Make a method Upload()
containing ion library to start the job. If you see in this method , i first declared the file,name, email,address
, for file
it gets the URI data of a selected file or the path file , then name, email,address
, it gets the data from EditText , you have to declare EditText to get the data.
b) Then identify the URL you want to use in Posting
mine is url_
. but make sure you include POST
, in the load section .load("POST",url_)
.
c) Then you set the file by using .setMultipartFile("File", file)
and the rest of the parameters by setting MultipartParameter .setMultipartParameter("Name", name)
and so on.
public void Upload() {
String file= FilePath.getPath(this, filePath);
final String name = name_edt.getText().toString();
final String email = email_edt.getText().toString();
final String address = address_edt.getText().toString();
final ProgressDialog pd;
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Logging in...");
pd.setCancelable(false);
pd.show();
final String url_ = "xxxxxxxxxxxxxxx";
final File file = new File(Uri.parse(path).toString());
Ion.with(MainActivity.this)
.load("POST",url_)
.progressDialog(pd)
.setMultipartFile("file", file)
.setMultipartParameter("Name", name)
.setMultipartParameter("Email", email)
.setMultipartParameter("Address.", address)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
// Toast.makeText(getApplicationContext(),""+file,Toast.LENGTH_LONG).show();
System.out.println("Error " + e);
// Toast.makeText(getApplicationContext(), "Exception : " + e, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(),""+e,Toast.LENGTH_LONG).show();
pd.dismiss();
}
});
}
Then you are good to go , according to your C# API , the file will be able to save in the Folder. Even others use Php
or any other language you can create your own Api to save a file in your server folder
Hope it works well.
Remove Thread from calling chain as Ion manages that for you and your exception is not related to Ion FYI.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedFilePath != null){
dialog = ProgressDialog.show(MainActivity.this,"","Uploading File...",true);
uploadFile(selectedFilePath);
// OnSendFileInfo();
}else{
Toast.makeText(MainActivity.this,"Please choose a File First",Toast.LENGTH_SHORT).show();
}
}
});
You can not use a View in Non Ui thread . So as i am seeing that you are creating a progress dialog inside your upload method which is being called for a thread .That code should be in Main thread only.