如何调试"不幸的是应用程序已停止"在Android?

时间:2016-11-28 05:43:14

标签: java android-studio

我是Android编程的新手。我按照youtube上的Android教程指南进行操作。这是我们的论文。

这是MainActivity

package com.example.abrico.violatorsprofile;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.kosalgeek.android.photoutil.CameraPhoto;
import com.kosalgeek.android.photoutil.ImageLoader;

import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getName();


    ImageView ivCamera, ivImage;
    CameraPhoto cameraPhoto;

    final int CAMERA_REQUEST= 23345;
    DataBaseHelper myDb;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myDb = new DataBaseHelper(this);

        //Move to TakePhoto activity
       Button btnPhoto = (Button) findViewById(R.id.btnPhoto);
        btnPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent = new Intent(getApplicationContext(),com.example.abrico.violatorsprofile.takephoto.class);
                startActivity(intent);
            }
        });



        //   Opening the camera

        ivImage = (ImageView)findViewById(R.id.ivImage);
        ivCamera = (ImageView)findViewById(R.id.ivCamera);
        cameraPhoto = new CameraPhoto(getApplicationContext());

        ivCamera.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                try {
                    startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST);
                    cameraPhoto.addToGallery();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(),
                            "Some wrong while taking photos", Toast.LENGTH_SHORT).show();
                }
            }
        });
        }

    @Override //* IMAGE VIEW
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode== RESULT_OK){
            if(requestCode==CAMERA_REQUEST){
                String photoPath = cameraPhoto.getPhotoPath();
                try {
                    Bitmap bitmap = ImageLoader.init().from(photoPath).requestSize(200,200).getBitmap();
                    ivImage.setImageBitmap(bitmap);

                }catch (FileNotFoundException e) {
                    Toast.makeText(getApplicationContext(),
                            "Some wrong while loading photos", Toast.LENGTH_SHORT).show();
                }
                Log.d(TAG, photoPath);
            }
        }










    }
    }

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abrico.violatorsprofile">

    <uses-feature
        android:name="android.hardware.camera2"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".takephoto">

        </activity>


    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

您将获得OutOfMemory例外。从相机加载大图像并加载到屏幕时,这是一个众所周知的问题。您应该在加载到ImageView

之前缩小该图像

以下是从Loading Large Bitmaps Efficiently

获取的示例代码
public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

使用android:largeHeap="true"的另一篇文章提到了另一个技巧。在开发Android应用程序时,这应该是最佳实践。

答案 1 :(得分:0)

您无法动态增加堆大小,但可以使用。

请求使用更多
android:largeHeap="true"
在manifest.xml中,您可以在清单中添加这些行,以便在某些情况下使用它。

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

告诉我这是否有用