在拍摄照片后在Android应用程序上的黑屏,但它连接到调试器时工作

问题描述 投票:1回答:1

我几天来一直在努力解决这个问题,似乎无法超越它。基本上我有一个活动,有3个图像视图,一个按钮启动相机意图

问题是,当我正在开发和运行调试器时,一切都运行得很好但不是当我单独运行应用程序(没有调试器,只是在手机上)接受图片预览后它只是显示黑屏并且在此之后什么都不做。

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

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

我认为这可能是一个异步问题,但我对android很新,并且不知道如何实现这一点,在我脑海中并通过我已阅读/观看的教程 - 这应该是一个简单的过程,但我似乎无法得到它上班

这是我在活动布局上的代码:

<?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);

    }

这是onActivityResult:

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();
        }
    }
android camera imageview photo
1个回答
0
投票

美好的一天,

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

压缩图像后,一切正常(预览和保存),我唯一需要改变的是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();;
}

}

这是savebitmap方法:

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.