2016-09-01 4 views
1

다음 활동은 사진을 찍어 저장 공간에 저장하고 활동의 ImageView에 표시합니다. 사진을 클릭하고 OK 버튼을 누르면 '불행히도 카메라가 멈췄다'라는 메시지가 나타나고 카메라 작동 화면이 표시됩니다. ImageView가 설정되지 않았습니다.활동이 충돌 함 (파일 처리 및 카메라)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:gravity="center"> 

<ImageView 
    android:layout_width="250dp" 
    android:layout_height="250dp" 
    android:id="@+id/ivCaptured" 
    android:src="@drawable/photo" 
    android:baselineAlignBottom="false" /> 

</LinearLayout> 

매니페스트 :

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

<uses-feature 
    android:name="android.hardware.camera" 
    android:required="true" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 

<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=".Home" /> 
    <activity android:name=".Camera" /> 
    <activity android:name=".History"></activity> 

    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.example.android.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true" 
     android:readPermission="com.company.app.fileprovider.READ"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths"></meta-data> 
    </provider> 

</application> 

</manifest> 

file_paths.xml 저장소에 저장을 클릭 이미지가

Camera.java

public class Camera extends AppCompatActivity { 

static final int REQUEST_TAKE_PHOTO = 1; 
String mCurrentPhotoPath; 
ImageView ivCaptured; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 
    ivCaptured = (ImageView) findViewById(R.id.ivCaptured); 
    dispatchTakePictureIntent(); 
} 

private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      Uri photoURI = FileProvider.getUriForFile(Camera.this, "com.example.android.fileprovider", photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
     //galleryAddPic(); 
     setPic(); 
    } 
} 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 
    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

/*private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
}*/ 

private void setPic() { 
    // Get the dimensions of the View 
    int targetW = ivCaptured.getLayoutParams().width; 
    int targetH = ivCaptured.getLayoutParams().height; 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outWidth; 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    ivCaptured.setImageBitmap(bitmap); 
} 
} 

activity_camera.xml (파일 탐색기와 검사) :

,

로그 캣 :

09-01 18:23:38.278 11159-11159/? D/dalvikvm: Late-enabling CheckJNI 
09-01 18:23:38.378 11159-11159/com.kjsce.beproject.samarthan I/ActivityThread: Pub com.example.android.fileprovider: android.support.v4.content.FileProvider 
09-01 18:23:38.438 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to resolve interface method 14517: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to resolve interface method 14521: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
09-01 18:23:38.442 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
09-01 18:23:38.494 11159-11159/com.kjsce.beproject.samarthan I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 
09-01 18:23:38.494 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to resolve virtual method 14417: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 
09-01 18:23:38.506 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007 
09-01 18:23:38.518 11159-11159/com.kjsce.beproject.samarthan I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
09-01 18:23:38.518 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to resolve virtual method 407: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
09-01 18:23:38.518 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
09-01 18:23:38.522 11159-11159/com.kjsce.beproject.samarthan I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
09-01 18:23:38.522 11159-11159/com.kjsce.beproject.samarthan W/dalvikvm: VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getType (I)I 
09-01 18:23:38.522 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
09-01 18:23:38.778 11159-11163/com.kjsce.beproject.samarthan D/dalvikvm: GC_CONCURRENT freed 207K, 3% free 10939K/11271K, paused 151ms+1ms, total 214ms 
09-01 18:23:38.814 11159-11159/com.kjsce.beproject.samarthan D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 
09-01 18:23:38.830 11159-11159/com.kjsce.beproject.samarthan D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
09-01 18:23:38.834 11159-11159/com.kjsce.beproject.samarthan D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
09-01 18:23:38.866 11159-11159/com.kjsce.beproject.samarthan W/EGL_genymotion: eglSurfaceAttrib not implemented 
09-01 18:23:38.874 11159-11159/com.kjsce.beproject.samarthan D/OpenGLRenderer: Enabling debug mode 0 
09-01 18:23:38.962 11159-11159/com.kjsce.beproject.samarthan D/OpenGLRenderer: TextureCache::get: create texture(0xb9615438): name, size, mSize = 2, 4096, 4096 
09-01 18:23:40.690 11159-11159/com.kjsce.beproject.samarthan W/EGL_genymotion: eglSurfaceAttrib not implemented 
09-01 18:23:41.906 11159-11159/com.kjsce.beproject.samarthan D/dalvikvm: GC_FOR_ALLOC freed 47K, 3% free 11140K/11399K, paused 4ms, total 4ms 
09-01 18:23:41.914 11159-11159/com.kjsce.beproject.samarthan D/OpenGLRenderer: TextureCache::flush: target size: 2457 
09-01 18:23:41.914 11159-11159/com.kjsce.beproject.samarthan D/OpenGLRenderer: TextureCache::callback: name, removed size, mSize = 2, 4096, 0 
09-01 18:23:46.990 11159-11159/com.kjsce.beproject.samarthan W/EGL_genymotion: eglSurfaceAttrib not implemented 
09-01 18:23:46.994 11159-11159/com.kjsce.beproject.samarthan D/OpenGLRenderer: TextureCache::get: create texture(0xb9615438): name, size, mSize = 6, 4096, 4096 
+0

logcat에서 스택 트레이스를 보여주십시오. –

답변

0

로그 캣에서 귀하의 정보가 the Java stack trace associated with your crash 표시되지 않습니다, 그래서 당신 또는 우리에게 도움이되지 않습니다.

다른 문제는 다음과 같습니다 당신은 "file:" + image.getAbsolutePath()mCurrentPhotoPath를 설정하고, 그 값은 파일 시스템 경로도 아니고 Uri도 아니다

  • .

  • photoFilenull 인 경우 사진을 찍지 않을 경우 setPic()으로 전화하십시오.

  • startActivityForResult()은 비동기이므로 사용자가 사진을 찍기 전에 setPic()으로 전화를 걸고 있습니다. setPic() 전화를 onActivityResult()으로 이동 한 다음 RESULT_OK (사용자가 사진을 찍었 으면 좋음을 나타냄)을 얻은 경우에만 이동하십시오.

0

카메라가 있는지 확인하지 않습니다. Commonsware에 맞춰서, 당신의 통나무는 충돌이 아닙니다. 다음은 공식 문서를 따르는 것 같습니다. 귀하의 코드가 theres와 정확히 일치하는지 확인하고 아래의 수표를 확인하십시오. 나는 개인적으로

int numCameras = Camera.getNumberOfCameras(); 
if (numCameras > 0) { 
    hasCamera = true; 
} 
명시 적 후방 카메라에 액세스하려고하면 당신은 여전히 ​​(당신 아수스 넥서스)를 찾고 충돌합니다 일부 장치에 말했다

하지만를 사용

Android docs

import android.content.pm.PackageManager; 

PackageManager pm = context.getPackageManager(); 

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
} 

이 답변의 범위를 벗어납니다.

나는 sudo 하드웨어에 액세스하는 데 매우 문제가되는 에뮬레이터에있는 것을보고이 모든 것을 언급합니다.