gpt4 book ai didi

java - 如何在Android中调试 "unfortunately app has stopped"?

转载 作者:行者123 更新时间:2023-12-01 09:12:21 27 4
gpt4 key购买 nike

我是 Android 编程新手。我正在遵循 youtube 上的 Android 教程指南。这是我们的论文。

这是主 Activity

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










}
}

这是 list

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

最佳答案

您收到 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 应用程序时的最佳实践,应该避免这种情况。

关于java - 如何在Android中调试 "unfortunately app has stopped"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com