Android位图压缩错误

Android bitmap compress error

我正在尝试从 android 和 post 拍摄照片到服务器,但是当我 运行 应用程序时,我可以访问相机,拍照并获取接受照片后崩溃,但应用程序应将图像转换为 base64,并将其发送到服务器。为什么? (错误在行 79:Encode_image class: bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

崩溃错误是:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/test123.jpg: open failed: EACCES (Permission denied)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.andrei.sendphotos, PID: 3870
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
                  at com.example.andrei.sendphotos.MainActivity$Encode_image.doInBackground(MainActivity.java:79)
                  at com.example.andrei.sendphotos.MainActivity$Encode_image.doInBackground(MainActivity.java:72)
                  at android.os.AsyncTask.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)

这是我的代码:

public class MainActivity extends AppCompatActivity {

private Button takePhotoButton;
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);

    takePhotoButton = (Button) findViewById(R.id.take_photo);
    takePhotoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        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() {
    image_name= "test123.jpg";
    //file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + image_name);
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 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... params) {

        bitmap = BitmapFactory.decodeFile(file_uri.getPath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100,  stream);


        //convert stream to byte array
        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://192.168.1.102/AndroidFileUpload/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);
}

}

您是否授予了写入外部存储的权限 在您的应用程序 AndroidManifest 中 ?

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

或者这一个用于从外部存储读取:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

或者如果您使用的是 android 6 或更高版本的设备,您应该在代码中明确请求许可...

希望这对您有所帮助

问题可能是因为您正在获取实际路径,但您需要实际路径