1

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.

madnomad10011
  • 341
  • 2
  • 6
  • 19
  • 1
    I guess imgToUpload is a Url have you checked is a fully qualified url ? Or if you are getting on device you may try to use as upload(java.io.File) it could clarify the problem. For that problem when the file is not reach to api it gives that error file is missing. – FZE Mar 15 '16 at 09:17
  • @FZE well thank you for the reply, Cloudinary Docs. suggest Uri something like ( (e.g., '/home/my_image.jpg') but what i got from "selectedImage" Toast is : media://external/images/411 missing the image type in the name (411.JPEG), plus i can not add "selectedImage" in Cloudinary upload, outside of the onActivityResult, and when i tried to Toast "imgToUpload" i only got "support.android.AppCompact ...... widget ...." so i guess it didnt set it to the selectedImage Uri as i requested within the onActivityResult, plus if it did, it will be missing the file type i guess ? – madnomad10011 Mar 15 '16 at 10:24

2 Answers2

1

You can also convert the ImageView result to a Base64 and send the string to Cloudinary.

Otherwise, you can provide any one of the types defined in https://github.com/cloudinary/cloudinary_java/blob/master/cloudinary-android/src/main/java/com/cloudinary/android/UploaderStrategy.java#L65-L77

Itay Taragano
  • 1,901
  • 1
  • 11
  • 12
0

Well, problem solved, for some reason getting the real Path of a file image is kind of tricky from one API to another, not sure why, or if there is another simple way to do that i didnt discover yet, but according to several posts such as: android get real path by Uri.getPath()

You can get the Actual Image path, from the Uri by converting it through the method given in the post, considering the API.

And since Cloudinary requirement to upload a file, is providing the actual path of the file on the device, or other means mentioned in their Docs. this obstacle must be tackled by acquiring the actual path of the file http://cloudinary.com/documentation/upload_images#api_example_1

The following is the code to upload a file to Cloudinary in Android Studio;

    @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(String.valueOf(vidToUpload));

            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImgPath = data.getData();
        RealImgPath = Uri.parse(getPath(selectedImgPath));
        Toast.makeText(getApplicationContext(), " " + RealImgPath, Toast.LENGTH_LONG).show();

    }

}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}


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", "xxxxxx");
        config.put("api_key", "xxxxxxxxxxxxxxx");
        config.put("api_secret", "xxxxxxxxxxxxxxxxxxx");
        Cloudinary cloudinary = new Cloudinary(config);
        try {
            cloudinary.uploader().upload(""+RealImgPath, ObjectUtils.asMap("resource_type", "auto"));
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressDialog.dismiss();
        super.onPostExecute(aVoid);
    }
}

I hope this could be useful to someone and spare them the discovery expedition i had to go through for several days just to achieve this, since Cloudinary Docs. kind of lacks some specific info, and Stakcoverflow lack some posts on Cloudinary specific posts, well considering that i am learning along the way is also prolonged this simple issue.

To be noted, the above code, still not perfect, since the progress dialog is still not functional properly ( pop up and freez - no progress) and the chosen picture from the intent gallery will not be displayed on the ImageView as it should be, need to fix this for now.

Community
  • 1
  • 1
madnomad10011
  • 341
  • 2
  • 6
  • 19