我在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);在对邮递员进行测试时,上面的代码工作得很好。
然后我编写了一些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;
}上面的代码不能工作,因为流明应用程序无法使用提供的路径获取图像,我显然犯了一个错误,但我不知道它是什么,也不知道如何修复它。
如果有人能解释我做错了什么/指出我做错了什么,那将很有帮助
发布于 2020-09-16 14:20:45
这个可能会派上用场,我做错了几件事
VolleyMultipartRequest multipartRequest =新VolleyMultipartRequest(Request.Method.POST,URLs.URL_UPLOAD_AD, )
@ Map getByteData() { Map params =新HashMap<>();long imageName = System.currentTimeMillis();params.put(“图片”,新DataPart(imageName + ".png",getFileDataFromDrawable(位图));mCoverImage.getDrawable(),“image/jpeg”);返回参数;}
@重写公共Map getHeaders()抛出AuthFailureError { HashMap headers = new HashMap();返回标头;}
发布于 2020-09-15 17:44:04
似乎你没有发送图像。在日志中,我看到了Missing required parameter - file。你能试着用图片网址(如https://res.cloudinary.com/demo/image/upload/v1561532539/sample.jpg)上传吗?
https://stackoverflow.com/questions/63892209
复制相似问题