【问题标题】:Cannot upload image Cloudinary - Android无法上传图片 Cloudinary - Android
【发布时间】:2017-02-23 21:34:52
【问题描述】:

我无法将 Android 的图像上传到 Cloudinary,我不知道为什么,尝试上传时出错。不要只发出任何消息 方法抛出 'java.io.IOException' 异常。

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

【问题讨论】:

  • 发布您的错误堆栈
  • 不要出错,只不要上传图片
  • 错误出现在你的最后一个 try catch 块中,该行正在抛出该错误
  • 尝试调试,上传前检查所有变量的值

标签: android image upload cloudinary


【解决方案1】:

上传调用需要服务器通信。 Android 不允许它在主 (UI) 线程上发生。 您可以尝试使用AsyncTask 包装上传调用。这样,上传调用在不同的线程上进行,不会干扰 UI。

这种包装的一个(非常)简单的例子 -

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

【讨论】:

    猜你喜欢
    • 2015-12-19
    • 2016-03-10
    • 2016-09-20
    • 2019-05-07
    • 2017-12-02
    • 2021-04-22
    • 2022-01-12
    • 2012-06-30
    • 2019-03-17
    相关资源
    最近更新 更多