Android将照片上传到数据库异步错误

时间:2017-03-23 22:15:39

标签: java android multithreading concurrency android-asynctask

我一直在尝试拍照然后将其上传到我的数据库,但是一旦拍摄照片,应用程序崩溃了。它看起来像是一些不同的问题,但我似乎无法弄清楚它们在哪里。

public class MainActivity extends AppCompatActivity {

  private Button button;
  private String encoded_string, image_name;
  private Bitmap bitmap;
  private File file;
  private Uri file_uri;

  @Override
  protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.btPhoto);
    button.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        getFileUri();
        i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
        startActivityForResult(i, 10);
      }

    });
  }

  private void getFileUri() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
    String currentTimeStamp = dateFormat.format(new Date());
    String image_name = "picture_" + currentTimeStamp + ".jpg";

    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
        + File.separator + image_name
    );

    file_uri = Uri.fromFile(file);
  }

  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);
      bitmap.recycle();

      byte[] array = stream.toByteArray();
      encoded_string = Base64.encodeToString(array, 0);
      return null;
    }

    protected void onPostExecute(Void aVoid){
      makeRequest();
    }
  }

  private void makeRequest(){
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest request = new StringRequest(Request.Method.POST, "http://www.houstonaduk.com/ImageFile.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);
  }
}

这是我当时收到的错误。我已经搜索了我的代码有什么问题,但我也是新手,所以Stackoverflow会给我一些有用的指示。

  

03-23 21:56:14.936 24471-25343 / com.app.jools.camera E / BitmapFactory:   无法解码流:java.io.FileNotFoundException:   /storage/emulated/0/Pictures/picture_20170323_21_56_10.jpg:open   失败:EACCES(权限被拒绝)03-23 21:56:14.946   24471-25343 / com.app.jools.camera E / AndroidRuntime:FATAL EXCEPTION:   AsyncTask#1                                                                         处理:com.app.jools.camera,PID:24471                                                                         java.lang.RuntimeException:执行时发生错误   doInBackground()                                                                             在android.os.AsyncTask $ 3.done(AsyncTask.java:309)                                                                             在   java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:223)                                                                             在java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                             在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)                                                                             在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                             在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588)                                                                             在java.lang.Thread.run(Thread.java:818)                                                                          引起:java.lang.NullPointerException:尝试调用虚拟   方法'布尔值   android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,   int,java.io.OutputStream)'在空对象引用上                                                                             在   com.app.jools.camera.MainActivity $ Encode_image.doInBackground(MainActivity.java:89)                                                                             在   com.app.jools.camera.MainActivity $ Encode_image.doInBackground(MainActivity.java:84)                                                                             在android.os.AsyncTask $ 2.call(AsyncTask.java:295)                                                                             在java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                             在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)                                                                             在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)                                                                             在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:588)                                                                             在java.lang.Thread.run(Thread.java:818)

感谢您提供任何帮助和指导。

1 个答案:

答案 0 :(得分:0)

这是因为您没有声明在 AndroidManifest.xml 中写入存储的权限,因为错误消息告诉:

  

03-23 21:56:14.936 24471-25343 / com.app.jools.camera E / BitmapFactory:   无法解码流:java.io.FileNotFoundException:   /storage/emulated/0/Pictures/picture_20170323_21_56_10.jpg:open   失败:EACCES(权限被拒绝)

所以你需要添加权限:

Manifest.permission.WRITE_EXTERNAL_STORAGE  

从API&gt; = 23开始,您需要在运行时管理权限,在Understanding App Permissions为其详细了解。