我使用排球库上传此图片。当我在邮递员上测试时,php文件工作正常。那么android中的代码有什么问题
我点击拍照按钮中的图像,然后在提交点击时提交。
这是整个代码
public class MainActivity extends AppCompatActivity {
private Button image_selector, submit;
TextView image;
String encoded_string, image_name = "image 1.jpg";
Bitmap bitmap;
File file;
String fileUrl = "http://192.168.2.8/image-upload-android/image-upload.php";
ImageView image_clicked;
Uri file_uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_selector = (Button)findViewById(R.id.image_select);
submit = (Button)findViewById(R.id.submit);
image = (TextView)findViewById(R.id.image_name);
image_clicked = (ImageView)findViewById(R.id.image_clicked);
image_selector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUr();
image.setText(image_name);
i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
startActivityForResult(i, 10);
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Encode_image().execute();
}
});
}
private void getFileUr() {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+
file.separator+ image_name);
file_uri = Uri.fromFile(file);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 10 && resultCode == RESULT_OK){
}
}
private class Encode_image extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... voids) {
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array,0);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
makeRequest();
}
}
private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, fileUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject jsonObject = new JSONObject();
try {
if(jsonObject.names().get(0).equals("success")){
Toast.makeText(MainActivity.this, "Success: "+jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Error: "+jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("encoded_string",encoded_string);
map.put("image_name",image_name);
return map;
}
};
requestQueue.add(request);
}}
答案 0 :(得分:0)
使用Android Networking。我创建了这个库,它具有齐射中缺少的所有功能。
AndroidNetworking.upload(url)
.addMultipartFile("image", new File(imagePath))
.setTag("uploadTest")
.setPriority(Priority.HIGH)
.build()
.setUploadProgressListener(new UploadProgressListener() {
@Override
public void onProgress(long bytesUploaded, long totalBytes) {
// do anything with progress
}
})
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// do anything with response
}
@Override
public void onError(ANError error) {
// handle error
}
});