4

Basically all I want do is pick an image from the gallery and then the same image upload it to my Cloudinary account, I've seen the documentation from Cloudinary but I don't really understand how it works therefore don't know how to implement that in my code.

Here is the link where I got the information in case someone need it... https://github.com/cloudinary/cloudinary_java/tree/master/cloudinary-android

And this is what I have so far...

upload_image.class

public class edit_profile_activity extends AppCompatActivity 
{
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;

    public void loadImagefromGallery(View view) 
    {

        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        try 
        {
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) 
            {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                ImageView imgView = (ImageView) findViewById(R.id.circleImageView );
                imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "You haven't picked Image",Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) 
        {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) .show();
        }
    }
}

Any help will be appreciated

Phantom_strike
  • 149
  • 2
  • 10

2 Answers2

0

u can use this simple uploading function for Cloudinary:

Cloudinary cloudinary = new Cloudinary(Constants.CLOUDINARY_URL);
    try {
        FileInputStream is = new FileInputStream(new File(filePath));
        Uploader uploader = cloudinary.uploader();
        Map map = uploader.upload(is, new HashMap());
        return map;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

further explanation is here:The order is like this: Take picture->save to file->upload to cloudinary

Community
  • 1
  • 1
Shobhit
  • 1,096
  • 11
  • 30
0

This is how I do it:

The gallery intent returns a contentprovider uri i use that and save the image to my local (app/local) directory.

lets say this:

  String local_uri = new SaveImageAsyncTask(cropImageView,
                                uri).execute().get();

then i use this uri to upload the image to cloudinary:

private void checkInternetAndUploadImage(final String uri) {
        if (Validator.isOnline(this)) {
            new UploadAsyncTask().execute(Uri.parse(uri));
        }
    }

Where my doInBackground has this code:

Map profilePicture = ObjectUtils.asMap("public_id", PreferencesManager.getInstance().getUserId() +
                            "/" + PreferencesManager.getInstance().getUserName() + "_PROFILE_PICTURE");
                    profilePicture.put("uploadPrefix", "http://api.cloudinary.com");
                    profilePicture.put("timestamp", System.currentTimeMillis() / 1000);
                    Map imageUploadResult = CloudinaryManager.getInstance(Activity_ImageViewer.this).uploader().
                            upload(new File(voids[0].getPath()),
                                    profilePicture);

                    if (imageUploadResult.containsKey("url") /*&& backUploadResult.containsKey("url")*/) {
                        PreferencesManager.getInstance().setUSER_PROFILE_PICTURE_URL(String.valueOf(imageUploadResult.get("url")));
                        //PreferencesManager.getInstance().setUserBackAssetUrl(String.valueOf(backUploadResult.get("url")));
                        result = true;
                    }
MadScientist
  • 2,134
  • 14
  • 27