如何将图像从 android 上传到 Cloudinary?

Posted

技术标签:

【中文标题】如何将图像从 android 上传到 Cloudinary?【英文标题】:How to upload an image from android to Cloudinary? 【发布时间】:2017-08-04 08:09:42 【问题描述】:

基本上我想做的就是从图库中挑选一张图片,然后将同一张图片上传到我的 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 我已经尝试实现该代码,但它不起作用:/ 它给出了什么错误 【参考方案1】:

你可以使用 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();
    

进一步的解释在这里:The order is like this: Take picture->save to file->upload to cloudinary

【讨论】:

【参考方案2】:

这就是我的做法:

图库意图返回我使用的 contentprovider uri,并将图像保存到我的本地 (app/local) 目录。

让我们这样说:

  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;
                    

【讨论】:

以上是关于如何将图像从 android 上传到 Cloudinary?的主要内容,如果未能解决你的问题,请参考以下文章

将图像从 imageview 上传到 Firebase Android

Android将图像从图库上传到服务器[关闭]

将图像从 Android 上传到 Amazon S3?

如何从android改造上传图像到服务器

如何使用 Retrofit 将图像文件从 Android 上传到 ASP.NET(Web Services-ASMX) 上传文件夹

Android Studio:将图像从 Android Studio 上传到 Cloudinary (https://cloudinary.com/)