0

I cannot upload a image of Android to Cloudinary, i dont know why, error when try to uploader. Dont make any message only Method threw 'java.io.IOException' exception.

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

if(resultCode == RESULT_OK){
     Bitmap imageBitmap = (Bitmap) extras.get("data");
          Uri uri = getImageUri(getApplicationContext(), imageBitmap);  
            InputStream in = null;    
            try {
                in = getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }   
            Map config = new HashMap();
            config.put("cloud_name", "...");
            config.put("api_key", "...");
            config.put("api_secret", "...");
            Cloudinary mobileCloudinary = new Cloudinary(config);    
            try {
                mobileCloudinary.uploader().upload(in, ObjectUtils.asMap("public_id", "sample_remote"));
            } catch (IOException e) {
                e.printStackTrace();
            }
}
Eric Retamero
  • 51
  • 1
  • 5

1 Answers1

0

The upload call requires server communication. Android doesn't allow it to happen on the main (UI) thread. You can try to wrap the upload call with an AsyncTask. That way, the upload call takes place on a different thread, not disturbing the UI.

A (very) simple example of such wrap -

class CloudinaryUpload extends AsyncTask<String, String, String> {


    protected String doInBackground(String... urls) {
        try{
            Map imageResult = cloudinary.uploader().upload("file", ObjectUtils.emptyMap());

        }catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

    protected void onPostExecute(String url) {
          //do something with imageResult
    }
}
Maor.G
  • 440
  • 2
  • 5