Android应用在拍照后显示黑屏,但在连接到调试器时有效 [英] Android app shows a black screen after taking a picture but works when connected to a debugger

查看:92
本文介绍了Android应用在拍照后显示黑屏,但在连接到调试器时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



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



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



这是我的代码ayout for the activity:



 <?  xml     version   =  1.0    encoding   =  utf-8  >  
< android.support。 constraint.ConstraintLayout xmlns:android = http://schemas.android.com/apk/res/android

< span class =code-attribute> xmlns:app = http://schemas.android。 com / apk / res-auto

xmlns:tools = http:/ /schemas.android.com/tools\"

< span class =code-attribute> android:layout_width = match_parent

android:layout_height = match_parent

工具:上下文 = .PhotoActivity >

< ImageView

android:id = @ + id / imgPhoto1

< span class =code-attribute> 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

< span class =code-attribute> 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

< span class =code-attribute> android:layout_width = 236dp

android:layout_height = 136dp

android:layout_marginTop = 8dp

app:layout_constraintEnd_toEndOf = parent

< span class =code-attribute>
app:layout_constraintStart_toStartOf = parent

app:layout_constraintTop_toBottomOf = @ + id / imgPhoto2

app:srcCompat = @ drawable / dds_logo / >

< 按钮

< span class =code-attribute> android:id = @ + id / btnTakePhoto

android:layout_width = wrap_content

android:layout_height = wrap_content

android:layout_marginBo ttom = 8dp

android:layout_marginEnd = 8dp

android:layout_marginStart = 8dp

android:layout_marginTop = 52dp

android:text = Take照片

app:layout_constraintBottom_toBottomOf = parent

app:layout_constraintEnd_toEndOf = parent

app:layout_constraintStart_toStartOf = parent

app:layout_constraintTop_toBottomOf = @ + id / imgPhoto3

< span class =code-attribute> app:layout_constraintVertical_bias = 1.0 / >

< / android.support.constr aint.ConstraintLayout >





这是代码对于onClick事件:

  public   void  onClick(查看v){
尝试 {
// 响应点击次数
如果(v.getId()== R.id.btnTakePhoto){
captureImage();
}
}
catch (例外情况)
{
ex.printStackTrace();
}
}





这是对意图的调用:



 私人  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 已启动); // 在此行插入一个断点!!
}
if (numPhotos< 2 ){
Bundle extras = data.getExtras();
位图imageBitmap =(位图)extras.get( data);

字符串 savedImageURL = MediaStore.Images.Media.insertImage(
getContentResolver(),
imageBitmap,
Serial
刷新验证照片
);

switch (numPhotos){
case 0
imgPhoto1.setImageBitmap(imageBitmap);
break ;
case 1
imgPhoto2.setImageBitmap(imageBitmap);
break ;
case 2
imgPhoto3.setImageBitmap(imageBitmap);
break ;
}
numPhotos ++;
}
其他
{
numPhotos = 0 ;
Toast.makeText(PhotoActivity。 this 最多的照片达到,Toast.LENGTH_LONG)。show();
Intent i = new Intent(getApplicationContext(),MenuActivity。 class );
startActivity(i);
完成();
}
}

catch (例外情况)
{
ex.printStackTrace( );
}
}





我的尝试:



我试过只保存图像而不是在imageview中显示但仍然没有运气。它仅在调试模式下连接到IDE时才有效,而不是在部署到设备时。

解决方案

您可以在android mobile中检查cat log并检查异常。

然后异常是内存超出你的imgPhoto1非常高的分辨率。

然后你可以压缩文件再次使用是这个问题,你可以解决它。 / blockquote>

美好的一天全部,



由于Pratishkumar Kushwaha在压缩图像后发表评论,我设法修复了应用程序,一切正常。



OnActivityResult:



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

尝试 {
Bundle extras = data.getExtras();
final 位图imageBitmap =(位图)extras.get( 数据);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60 ,bytes);
byte [] byteArray = bytes.toByteArray();
final 位图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;
}


I have been struggling with this problem for a few days now and cannot seem to get past it. Basically I have an activity that has 3 image views with a button that starts the camera intent

The problem is that while I am developing and running through the debugger, everything works perfectly but not when I run the app alone (no debugger, just on the phone) after accepting the picture preview it just shows a black screen and does nothing after that.

All that is needed is for the picture to display in the image views and save to the gallery, it does all of this while in debug mode but not when I need to run the app independently.

I am completely at a loss with this problem - please can someone / anyone assist me.

I think this may be an async issue but I am very new to android and dont know how to implement this, in my head and through the tutorials I have read / watched - this should be an easy process, however I cannot seem to get it to work

Here is my code on the layout for the 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>



Here is the code for the onClick event:

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



Here is the call to the intent:

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

    }



Here is the 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();
        }
    }



What I have tried:

I have tried only saving the image and not displaying it in the imageview but still no luck. It still only works when connected to the IDE in debug mode and not when deployed to the device.

解决方案

You can check the cat log in android mobile and check exception.
Then the Exception is Memory out of exception than your imgPhoto1 is very high resolution.
Then You can compress the file and again use is this problem can you solve it.


Good Day All,

I have managed to fix the application thanks to the comment by Pratishkumar Kushwaha, after compressing the image, everything works fine.

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;
   }


这篇关于Android应用在拍照后显示黑屏,但在连接到调试器时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆