I am facing some kind of Error which after going over Cloudinary Docs., and several other post here, and other website, plus reading the stack trace i assume my Syntax for Uploading an image to the server is not how it should be, although i understand clearly from the examples in the Docs, i couldn't apply the right way according to the Error from stacktrace.
My stack trace gives me this Error: Caused by: java.lang.RuntimeException: Missing required parameter - file
My Code is as follow :
public class Upload extends AppCompatActivity implements View.OnClickListener {
public static final int RESULT_LOAD_IMAGE = 1;
ImageView imgToUpload;
Button buttonUploadSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
imgToUpload = (ImageView) findViewById(R.id.imgToUpload);
buttonUploadSubmit = (Button) findViewById(R.id.ButtonUploadSubmit);
imgToUpload.setOnClickListener(this);
buttonUploadSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgToUpload:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
case R.id.ButtonUploadSubmit:
UploadTask uploadTask = new UploadTask();
uploadTask.execute();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedVideo = data.getData();
imgToUpload.setImageURI(selectedVideo);
Toast.makeText(getApplicationContext(), selectedVideo.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), imgToUpload.toString(), Toast.LENGTH_LONG).show();
}
class UploadTask extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Upload.this);
progressDialog.setTitle("Upload in Progress ...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
Map config = new HashMap();
config.put("cloud_name", "xxxxxxx");
config.put("api_key", "xxxxxxxxxxxxxx");
config.put("api_secret", "xxxxxxxxxxx");
Cloudinary cloudinary = new Cloudinary(config);
try {
cloudinary.uploader().upload(imgToUpload, ObjectUtils.asMap("resource_type", "auto"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
} I guess that imgToUpload, is not accepted in cloudaniry upload method :
cloudinary.uploader().upload( imgToUpload,
ObjectUtils.asMap("resource_type", "auto"));
since its not a"file " and that is what i am missing according to stacktrace, thus i would appreciate it if someone could confirm this, and please guid me on the right syntax to this method, or the way to create a file with the chosen picture from the ImageView and including that file in the Cloudinary upload method ?
Stacktrace:
03-15 14:32:14.260 17683-20341/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: net.we4x4.we4x4, PID: 17683
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Missing required parameter - file
at com.cloudinary.android.UploaderStrategy.callApi(UploaderStrategy.java:106)
at com.cloudinary.Uploader.callApi(Uploader.java:22)
at com.cloudinary.Uploader.upload(Uploader.java:55)
at net.we4x4.we4x4.Upload$UploadTask.doInBackground(Upload.java:100)
at net.we4x4.we4x4.Upload$UploadTask.doInBackground(Upload.java:77)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
So i was able to Upload apicture, providing the following path : /storage/emulated/0/Pictures/Screenshots/Screenshot_2016-03-09-15-40-13.png
but by writing it manually into the Upload method, and i got the code from my the phone manually, just viewing the details of the picture, thus my issue is now is How to get this path of the displayed picture in the ImageView ?
Also the progressBar just frove, on 0 and did not change, i had to check the website/server to see that the picture got uploaded ... and i am not sure what is causing the progress bar to not function/freez !?
I would appreciate any guidance please.