简体   繁体   English

尝试保存在内部位图图片中

[英]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.该应用程序是当会员单击 btn 相机打开并拍照并在图像视图中查看时。 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/问题是当我在拍照后单击 btn 并单击 V 时,应用程序崩溃/

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. NullPointerException发生是因为您的bitmap实例为空。 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).不确定您如何使用此代码,但您可以添加一个空检查以确保bitmap每次执行操作时都不是空的(例如压缩它)。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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