如何将Android手机中的图片上传至Cloudinary?

4

我想要做的事情很简单,就是从相册中选择一张图片,并将选定的图片上传到我的Cloudinary账户。我看过了Cloudinary的文档,但是不太明白它的工作原理,因此不知道如何在我的代码中实现。

以下是提供信息的链接,如果有需要的话可以查看... https://github.com/cloudinary/cloudinary_java/tree/master/cloudinary-android

这是我目前的代码...

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();
        }
    }
}

任何帮助都将不胜感激。

请阅读文档,他们已经清楚地提到了。 - Jitesh Mohite
@jitesh mohite,我已经尝试实现了那段代码,但是它没有起作用 :/ - Phantom_strike
它给出了什么错误? - Jitesh Mohite
2个回答

0

您可以使用这个简单的上传函数来使用 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();
    }

进一步的说明在这里: 顺序如下:拍照->保存到文件->上传到 cloudinary

0

这是我的做法:

图库意图返回一个内容提供者URI,我使用它并将图像保存到我的本地(应用程序/本地)目录。

比方说:

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

然后我使用这个URI将图片上传到Cloudinary:

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

我的doInBackground有这段代码:

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;
                    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接