【问题标题】:How to upload an image file through android to cloudinary using lumen如何使用流明通过android将图像文件上传到cloudinary
【发布时间】:2021-01-01 15:24:34
【问题描述】:

我在 Lumen 中编写了一些代码来将图像上传到 cloudinary,该代码在使用 postman 测试时有效。

现在我正在尝试通过 android 应用而不是通过邮递员上传图片,但由于某种原因这不起作用。

下面是我的 Lumen 应用的图片上传代码

    $image_name = $request->file('picture')->getRealPath();
    
    $cloudder = Cloudder::upload($image_name, null, [
        'folder' => '/dog-lovers',
        'discard_original_filename' => true,
    ]);
    $uploadResult = $cloudder->getResult();
    $file_url = $uploadResult["url"];
    
    $input = $request->all();
    $input['picture'] = $file_url;
    $ad = Ad::create($input);
    return array('error'=>false, 'message'=>'ad created successfully', 'data'=>$ad);

上面的代码在 postman 上测试时完美运行。

然后我编写了一些 android 代码将图像从我的手机传递到 Lumen

    uploadImage.setOnClickListener(v -> {
        Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
        getIntent.setType("image/*");

        Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        pickIntent.setType("image/*");

        Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

        startActivityForResult(chooserIntent, PICK_IMAGE);
    });

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImageUri = data.getData();
        picturePath = getPath(getApplicationContext(), selectedImageUri);
        Log.i("UploadAdActivity", picturePath);
        Toast.makeText(this,picturePath, Toast.LENGTH_LONG).show();
    }
}

public static String getPath(Context context, Uri uri ) {
    String result = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
    if(cursor != null){
        if ( cursor.moveToFirst( ) ) {
            int column_index = cursor.getColumnIndexOrThrow( proj[0] );
            result = cursor.getString( column_index );
        }
        cursor.close( );
    }
    if(result == null) {
        result = "Not found";
    }
    return result;
}

protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("picture", picturePath);
                return params;
            }

上面的代码不起作用,因为 lumen 应用程序无法使用提供的路径获取图像,我显然在某个地方犯了错误,但我不知道它是什么,也不知道如何修复它.

如果有人可以解释该做什么/指出我做错了什么,那将非常有帮助

【问题讨论】:

  • 我使用 volley multipart 解决了这个问题

标签: java laravel lumen cloudinary


【解决方案1】:

您似乎没有发送图像。在我们的日志中,我看到了Missing required parameter - file。 您可以尝试使用图片 URL 上传(例如,https://res.cloudinary.com/demo/image/upload/v1561532539/sample.jpg)吗?

【讨论】:

    【解决方案2】:

    这可能对某人有用,我做错了一些事情

    1. 我使用的是 Volley 字符串请求而不是 Volley Multipart 请求

      VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, URLs.URL_UPLOAD_AD,
      
    2. 您需要使用 ByteData 传递图片/文件

      @Override
              protected Map<String, VolleyMultipartRequest.DataPart> getByteData() {
                  Map<String, VolleyMultipartRequest.DataPart> params = new HashMap<>();
      
                  long imageName = System.currentTimeMillis();
                  params.put("picture", new DataPart(imageName + ".png", getFileDataFromDrawable(bitmap))); mCoverImage.getDrawable()), "image/jpeg"));
      
                  return params;
              }
      
    3. 您需要使用设置标题

      @Override
                  public Map<String, String> getHeaders() throws AuthFailureError {
                      HashMap<String, String> headers = new HashMap<String, String>();
                      return headers;
                  }
      

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2018-03-15
      • 2015-12-25
      • 2021-03-08
      • 2013-10-26
      • 2021-03-20
      • 2020-05-02
      • 2016-02-02
      • 2018-07-29
      相关资源
      最近更新 更多