简体   繁体   English

如何使用流明通过android将图像文件上传到cloudinary

[英]How to upload an image file through android to cloudinary using lumen

I wrote some code in Lumen to upload images to cloudinary, the code works while testing using postman.我在 Lumen 中编写了一些代码将图像上传到 cloudinary,该代码在使用邮递员进行测试时有效。

Right now i'm trying to upload the image via an android app rather than via postman, but that doesn't work for some reason.现在我正在尝试通过 android 应用程序而不是通过邮递员上传图像,但这由于某种原因不起作用。

Below is the image upload code from my Lumen app下面是来自我的 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);

The above code works perfectly while testing on postman.上面的代码在邮递员上测试时完美运行。

I then wrote some android code to pass the image from my phone to Lumen然后我写了一些 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;
            }

The above code doesn't work as the lumen app isn't able to get the image using the path provided, I've obviously made a mistake somewhere, but I don't know what it is nor how to fix it.上面的代码不起作用,因为 lumen 应用程序无法使用提供的路径获取图像,我显然在某处犯了错误,但我不知道它是什么也不知道如何修复它。

It would be really helpful if someone could explain what to do/ point out what i'm doing wrong如果有人可以解释该做什么/指出我做错了什么,那将非常有帮助

It seems that you are not sending the image.您似乎没有发送图像。 in our logs, I saw Missing required parameter - file .在我们的日志中,我看到Missing required parameter - file Can you try upload using image URL (eg, https://res.cloudinary.com/demo/image/upload/v1561532539/sample.jpg )?您可以尝试使用图片 URL 上传吗(例如https://res.cloudinary.com/demo/image/upload/v1561532539/sample.jpg )?

Well this might come in handy to someone, I was doing a few things wrong好吧,这可能对某人有用,我做错了一些事情

  1. I was using a Volley String Request instead of a Volley Multipart request我使用的是 Volley String Request 而不是 Volley Multipart 请求

    VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, URLs.URL_UPLOAD_AD,
  2. you need to pass the picture/file using ByteData您需要使用 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. You need to set the Headers using您需要使用设置标题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM