首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过android将图像文件上传到cloudinary中

问如何通过android将图像文件上传到cloudinary中
EN

Stack Overflow用户
提问于 2020-09-14 21:45:07
回答 2查看 175关注 0票数 0

我在Lumen中编写了一些代码来将图像上传到cloudinary,这些代码在使用postman进行测试时工作。

目前,我正试图通过android应用程序而不是邮递员上传照片,但由于某些原因,这是行不通的。

下面是我的Lumen应用程序的图像上传代码

代码语言:javascript
复制
    $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);

在对邮递员进行测试时,上面的代码工作得很好。

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

代码语言:javascript
复制
    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;
            }

上面的代码不能工作,因为流明应用程序无法使用提供的路径获取图像,我显然犯了一个错误,但我不知道它是什么,也不知道如何修复它。

如果有人能解释我做错了什么/指出我做错了什么,那将很有帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-16 14:20:45

这个可能会派上用场,我做错了几件事

  1. I是使用Volley字符串请求而不是Volley多部分请求。

VolleyMultipartRequest multipartRequest =新VolleyMultipartRequest(Request.Method.POST,URLs.URL_UPLOAD_AD, )

  1. 您需要使用ByteData传递图片/文件

@ Map getByteData() { Map params =新HashMap<>();long imageName = System.currentTimeMillis();params.put(“图片”,新DataPart(imageName + ".png",getFileDataFromDrawable(位图));mCoverImage.getDrawable(),“image/jpeg”);返回参数;}

  1. ,您需要使用

@重写公共Map getHeaders()抛出AuthFailureError { HashMap headers = new HashMap();返回标头;}

票数 0
EN

Stack Overflow用户

发布于 2020-09-15 17:44:04

似乎你没有发送图像。在日志中,我看到了Missing required parameter - file。你能试着用图片网址(如https://res.cloudinary.com/demo/image/upload/v1561532539/sample.jpg)上传吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63892209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档