2016-11-28 44 views
0

我是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> 
+0

是什麼問題?發佈logcat文件 –

+0

發佈logcat文件 –

+0

已發佈對不起,我新在這裏 –

回答

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

告訴我,這沒有奏效

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應用程序時作爲最佳實踐。