Android 应用程序在激活时崩溃Galaxy S4的结果

Android 应用程序在激活时崩溃Galaxy S4的结果,android,camera,Android,Camera,我有一个活动,它拍摄一张照片,然后用捕获的图像设置一个imageview,然后将其上传到parse。 它在LG G4上运行得非常好,但在galaxy S4上,当我按下显示我想要这张图片的按钮后,它在OnActivityResult函数中崩溃。 我在日志中看到有一个nullpointerexception,我想是因为星系s4的内存比LG G4少。 我想我理解这个问题(我对记忆的假设),但不知道如何解决它 活动 import android.content.Intent; import androi

我有一个活动,它拍摄一张照片,然后用捕获的图像设置一个imageview,然后将其上传到parse。 它在LG G4上运行得非常好,但在galaxy S4上,当我按下显示我想要这张图片的按钮后,它在OnActivityResult函数中崩溃。 我在日志中看到有一个nullpointerexception,我想是因为星系s4的内存比LG G4少。 我想我理解这个问题(我对记忆的假设),但不知道如何解决它

活动

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SituationStatusActivity extends AppCompatActivity {

    ImageView imVCature_pic_nir;
    ImageView imVCature_pic_sharon;

    private static final int REQUEST_CODE = 1;

    String imageFilePath;
    public SharedPreferences startExplePref;
    public String user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_situation_status);
        if(savedInstanceState!=null){
            imageFilePath = savedInstanceState.getString("path");
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        overridePendingTransition(R.anim.fadein, R.anim.fadeout);

//       get the user
        startExplePref = PreferenceManager.getDefaultSharedPreferences(this);
        user = startExplePref.getString("user", "");
//        upload nir Pic

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imageFilePath = Environment.getExternalStorageDirectory().toString() + "/Android/data/com.my.app/Image-" + timeStamp + ".png";

        imVCature_pic_nir = (ImageView) findViewById(R.id.nirPic);
        imVCature_pic_sharon = (ImageView) findViewById(R.id.sharonPic);

        Button uploadPic = (Button) findViewById(R.id.picUploadButton);
        uploadPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    /*************************** Camera Intent Start ************************/
                File imageFile = new File(imageFilePath);
                Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
                // Standard Intent action that can be sent to have the camera
                // application capture an image and return it.
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);   // set the image file name
                startActivityForResult(intent, 1);
                /*************************** Camera Intent End ************************/
            }
        });

        UpdatePic();

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putString("path", imageFilePath);

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
// if picture was taken
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            bmpFactoryOptions.inSampleSize = 5;
            bmpFactoryOptions.inJustDecodeBounds = false;

            //imageFilePath image path which you pass with intent
            Bitmap bp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

            //rotate image by 90 degrees
            Matrix rotateMatrix = new Matrix();
            rotateMatrix.postRotate(90);
            Bitmap rotatedBitmap = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), rotateMatrix, false);
//
            Drawable d = new BitmapDrawable(rotatedBitmap);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
//            // Compress image to lower quality scale 1 - 100
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] image = stream.toByteArray();
//
//            // Create the ParseFile
            ParseFile file = new ParseFile("image.png", image);
//            // Upload the image into Parse Cloud
            file.saveInBackground();
//
//            // Create a New Class called "ImageUpload" in Parse
            ParseObject SituationPic = new ParseObject("SituationPic");
//
//            // Create a column named "ImageName" and set the string
            SituationPic.put("ImageName", "First Pic");
//
            SituationPic.put("user", user);

//            // Create a column named "ImageFile" and insert the image
            SituationPic.put("ImageFile", file);
//
//            // Create the class and the columns
            SituationPic.saveInBackground();

            if (user.equals("nir")) {
                imVCature_pic_nir.setImageBitmap(getRoundedCornerBitmap(rotatedBitmap, 500));
                imVCature_pic_nir.setScaleType(ImageView.ScaleType.FIT_XY);

            } else if (user.equals("sharon")) {
                imVCature_pic_sharon.setImageBitmap(getRoundedCornerBitmap(rotatedBitmap, 500));
                imVCature_pic_sharon.setScaleType(ImageView.ScaleType.FIT_XY);

            } else {
                Toast.makeText(SituationStatusActivity.this, "error", Toast.LENGTH_LONG).show();
            }

//       if picture was not taken
        } else {
            Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_LONG).show();
        }
//        UpdatePic();
    }


    public Bitmap nirBitmap;
    public Bitmap sharonBitmap;
    //change it to load in the beginiing
    public void UpdatePic()
    {
//        Update photo of nir
        ParseQuery<ParseObject> querynir = ParseQuery.getQuery("SituationPic");
        querynir.orderByDescending("updatedAt");
        querynir.whereEqualTo("user", "nir");
        querynir.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                ParseObject object = objects.get(0);
                ParseFile applicantResume = (ParseFile) object.get("ImageFile");
                applicantResume.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
                                    data.length);
                            Matrix rotateMatrix = new Matrix();
                            rotateMatrix.postRotate(360);
                            Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), rotateMatrix, false);
                            nirBitmap = rotatedBitmap;
                            imVCature_pic_nir.setImageBitmap(getRoundedCornerBitmap(rotatedBitmap, 500));
                            imVCature_pic_nir.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                    }
                });
            }
        });

        //        Update photo of sharon
        ParseQuery<ParseObject> querySHN = ParseQuery.getQuery("SituationPic");
        querySHN.orderByDescending("updatedAt");
        querySHN.whereEqualTo("user", "sharon");
        querySHN.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                ParseObject object = objects.get(0);

                ParseFile applicantResume = (ParseFile) object.get("ImageFile");
                applicantResume.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0,
                                    data.length);
                            Matrix rotateMatrix = new Matrix();
                            rotateMatrix.postRotate(180);
                            Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), rotateMatrix, false);

                            sharonBitmap = rotatedBitmap;


                            imVCature_pic_sharon.setImageBitmap(getRoundedCornerBitmap(rotatedBitmap, 500));
                            imVCature_pic_sharon.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                    }
                });
            }
        });

    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }


}

在使用解码后的位图进行任何工作之前,您需要检查它是否真的解码良好

// Code above
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
    BitmapFactory.Options bmpFactoryOptions = new mapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    bmpFactoryOptions.inSampleSize = 5;
    bmpFactoryOptions.inJustDecodeBounds = false;

    //imageFilePath image path which you pass with intent
    Bitmap bp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    if (bp != null) {
        //rotate image by 90 degrees
        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postRotate(90);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), rotateMatrix, false);
    }
// Code below
// Code above
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
    BitmapFactory.Options bmpFactoryOptions = new mapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    bmpFactoryOptions.inSampleSize = 5;
    bmpFactoryOptions.inJustDecodeBounds = false;

    //imageFilePath image path which you pass with intent
    Bitmap bp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    if (bp != null) {
        //rotate image by 90 degrees
        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postRotate(90);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), rotateMatrix, false);
    }
// Code below