拍照后 android 应用出现黑屏,但在连接到调试器后可以正常工作

Black screen on android app after taking a picture but it works when connected to a debugger

几天来我一直在为这个问题苦苦挣扎,似乎无法克服它。基本上我有一个 activity,它有 3 个图像视图和一个启动相机 intent

的按钮

问题是,当我通过调试器进行开发和 运行 时,一切正常,但当我 运行 单独使用应用程序时(没有调试器,仅在 phone) 接受图片预览后,它只是显示黑屏,之后什么都不做。

所需要的只是图片显示在图像视图中并保存到图库中,它在调试模式下完成所有这些,但当我需要独立 运行 应用程序时则不需要。

我对这个问题完全不知所措 - 请有人/任何人帮助我。

我认为这可能是一个异步问题,但我是 android 的新手,不知道如何实现它,在我的脑海中以及通过我阅读/观看的教程 - 这应该很容易过程,但是我似乎无法让它工作

这是我在 activity:

布局上的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PhotoActivity">

    <ImageView
        android:id="@+id/imgPhoto1"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/dds_logo" />

    <ImageView
        android:id="@+id/imgPhoto2"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto1"
        app:srcCompat="@drawable/dds_logo" />

    <ImageView
        android:id="@+id/imgPhoto3"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto2"
        app:srcCompat="@drawable/dds_logo" />

    <Button
        android:id="@+id/btnTakePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="52dp"
        android:text="Take Photo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto3"
        app:layout_constraintVertical_bias="1.0" />

</android.support.constraint.ConstraintLayout>

这是 onClick 事件的代码:

public void onClick(View v) {
        try {
            //respond to clicks
            if (v.getId() == R.id.btnTakePhoto) {
                captureImage();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

这里是对意图的调用:

private void captureImage() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST);

    }

这是 onActivtyResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try{

            if (!Debug.isDebuggerConnected()){
                Debug.waitForDebugger();
                Log.d("debug", "started"); // Insert a breakpoint at this line!!
            }
            if(numPhotos < 2) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

                String savedImageURL = MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        imageBitmap,
                        "Serial",
                        "Refresh Verification photo"
                );

                switch (numPhotos) {
                    case 0:
                        imgPhoto1.setImageBitmap(imageBitmap);
                        break;
                    case 1:
                        imgPhoto2.setImageBitmap(imageBitmap);
                        break;
                    case 2:
                        imgPhoto3.setImageBitmap(imageBitmap);
                        break;
                }
                numPhotos++;
            }
            else
            {
                numPhotos = 0;
                Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
                Intent i = new Intent(getApplicationContext(), MenuActivity.class);
                startActivity(i);
                finish();
            }
        }

        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

大家好,

我设法解决了这个问题,这是因为相机拍摄的图像质量太高了。

压缩图像后,一切正常(预览和保存),我唯一需要更改的是 OnActivityResult 方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

try {
    Bundle extras = data.getExtras();
    final Bitmap imageBitmap = (Bitmap) extras.get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
    byte[] byteArray = bytes.toByteArray();
    final Bitmap compressedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    if (numPhotos < 3) {

        switch (numPhotos) {
            case 0:
                imgPhoto1.setImageBitmap(compressedBitmap);
                savebitmap(compressedBitmap);
                numPhotos++;
                break;
            case 1:
                imgPhoto2.setImageBitmap(compressedBitmap);
                savebitmap(compressedBitmap);
                numPhotos++;
                break;
            case 2:
                imgPhoto3.setImageBitmap(compressedBitmap);
                savebitmap(compressedBitmap);
                numPhotos = 0;
                Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
                Intent i = new Intent(getApplicationContext(), MenuActivity.class);
                startActivity(i);
                finish();
                break;

        }
    } else {
        numPhotos = 0;
        Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
        Intent i = new Intent(getApplicationContext(), MenuActivity.class);
        startActivity(i);
        finish();
    }
}
catch(Exception e){
    e.printStackTrace();;
}

}

这里是保存位图的方法:

public static File savebitmap(Bitmap bmp) throws IOException {

        String date = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date());

        File folder = new File(Environment.getExternalStorageDirectory()
                + "/RefreshPhotos");

        boolean var = false;
        if (!folder.exists())
            var = folder.mkdir();

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(folder
                + File.separator + "RefreshPhoto_" + date + ".png");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
        return f;
    }