简体   繁体   中英

try to save in internal bitmap picture

the app is when the member click the btn camera is open and take pic and the see it in the image view. and when he close the app the picture need to save in the internal storage. the problem is when i click in the btn and click V after i take photo the app is crash/

package com.e.test;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

global variables:

    final int CAMERA_REQUEST = 1;
    ImageView imageview;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

this is the load image:

        try {
            FileInputStream fis = openFileInput("pictures");
            bitmap = BitmapFactory.decodeStream(fis);
            imageview.setImageBitmap(bitmap);
            fis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button btn = findViewById(R.id.btn);
        imageview = findViewById(R.id.image_result);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,CAMERA_REQUEST);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
        {
            bitmap = (Bitmap)data.getExtras().get("data");
            imageview.setImageBitmap(bitmap);
        }
    }

this is the save:

    @Override
    protected void onPause() {
        super.onPause();
        try {
            FileOutputStream fos = openFileOutput("pictures",MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.JPEG,50,fos);
            fos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

The NullPointerException occurs because your bitmap instance is null. Not sure how you are using this code but you can add a null check to ensure that the bitmap is not null evrytime you perform operations on it (eg compressing it). Then your app will not crash. like this:

@Override
protected void onPause() {
    super.onPause();
    try {
        if(bitmap != null){
            FileOutputStream fos = openFileOutput("pictures",MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.JPEG,50,fos);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM