这是我使用Android Studio的Android上传图片代码。我的应用程序在捕获图像后崩溃(应用程序已停止工作),但它在虚拟仿真器(同样的API 19)中表现良好,并且logcat告诉我在执行后台时发生错误。有人可以帮我找出我的错误吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
startActivityForResult(i,10);
}
});
}
private void getFileUri() {
image_name = "testing123.jpg";
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)
{
new Encode_image().execute();
}
}
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, "http://mygplobang.com/tut_connection.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, 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);
}
这是错误输出:
致命异常:AsyncTask#1 过程:com.example.jeffreykhang.kas,PID:6879 java.lang.RuntimeException:执行时发生错误 doInBackground() 在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 在 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 在java.util.concurrent.FutureTask.run(FutureTask.java:242) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841) 引起:java.lang.NullPointerException 在 com.example.jeffreykhang.kas.MainActivity $ Encode_image.doInBackground(MainActivity.java:93) 在 com.example.jeffreykhang.kas.MainActivity $ Encode_image.doInBackground(MainActivity.java:90) 在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841)