我正在开发一款Android应用。现在,我遇到文件上传问题。我在后端有一个功能,用图像创建项目。我正在使用存储库模式,所以我可以共享API的功能。在我以这种方式上传文件之前。
我的文件上传活动:
public class CreateItemActivity extends Activity {
Button btnChose;
Button btnUpload;
Bitmap bitmap;
ImageView imageView;
private String KEY_IMAGE = "image";
private String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_item);
btnChose = (Button)findViewById(R.id.buttonChoose);
btnUpload = (Button)findViewById(R.id.buttonUpload);
btnChose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
imageView = (ImageView)findViewById(R.id.imageView);
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
}
private void showFileChooser()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose image"), 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, getResources().getString(R.string.api_end_point)+"android-upload",
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(getBaseContext(), "File uploaded" , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(getBaseContext(), "Error listener", Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = "android_uploaded_image";
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, name);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
}'
这只是文件上传的测试活动。它工作正常。它将文件上传到服务器。但问题是我必须在服务器上传文件,如下所示。我正在使用PHP作为后端。
<?php file_put_contents(time().".png", base64_decode($_POST['image']); ?>
正如您在上面所看到的,我无法检索$_FILES['image']
之类的文件。相反,我必须使用$_POST
。我的问题是我已经在使用$_FILES
的服务器中进行文件上传功能。所以我想提出multipart/form-data
请求。我搜索文章。但这些都是不完整的,因为代码中的某些组件无法使用。
例如,对于此链接Trouble Sending Multipart File with Boundary via Volley,我无法使用MultipartBuilder。如何在android中使用volley轻松制作multipart / form-data请求。我正在使用最新的API版本,因此有些功能和组件不能像我提到的链接那样使用。