【问题标题】:How to upload an image from android to Cloudinary?如何将图像从 android 上传到 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 我已经尝试实现该代码,但它不起作用:/
  • 它给出了什么错误

标签: android android-studio cloudinary


【解决方案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;
                        }
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 2016-04-28
      • 2017-12-01
      • 2019-01-13
      • 2021-03-08
      • 2021-03-20
      • 2018-07-29
      • 2019-04-24
      • 2015-02-21
      相关资源
      最近更新 更多