我正在编写和使用相机的Android应用程序。一旦活动保存并且图像和另一个活动处理它。以下是保存图像的活动的代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class TakePictureActivity extends Activity implements
SurfaceHolder.Callback {
private Camera mCamera;
private Button takePictureButton;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private boolean mPreviewRunning;
private String currentPictureName;
private String sdcardPath = "/sdcard/DCIM";
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera c) {
currentPictureName = Long.toString(System.currentTimeMillis())
+ ".jpg";
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(sdcardPath
+ currentPictureName);
outStream.write(data);
outStream.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
} catch (Exception e) {
e.printStackTrace();
} finally {
mCamera.startPreview();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.takepicture);
mSurfaceView = (SurfaceView) findViewById(R.id.preview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
takePictureButton = (Button) findViewById(R.id.buttonTakePhoto);
takePictureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCamera.takePicture(null, null, mPictureCallback);
}
});
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
Size s = getOptimalPreviewSize(previewSizes, w, h);
p.setPreviewSize(s.width, s.height);
// p.setPreviewSize(s.height, s.width);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
mCamera.takePicture(null, null, mPictureCallback);
return true;
}
return false;
}
public void snapClicked(View view) {
Log.e("@@@@@@@@@", "snapclicked called");
mCamera.takePicture(null, null, mPictureCallback);
Toast.makeText(this, "Picture saved on sd card", Toast.LENGTH_SHORT)
.show();
}
}
问题是,当我使用这个应用程序时,相机服务在两三张照片后崩溃,我的应用程序崩溃了。它确实保存了少量图片。崩溃后,内置的相机应用程序无法打开。这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apps.objectdetection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ObjectDetection" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TakePictureActivity" />
<activity android:name=".AnalyzeActivity" />
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>
我在这里缺少什么?
修改 添加logcat输出
I/PackageManager( 69): Removing non-system package:com.apps.objectdetection
I/ActivityManager( 69): Force stopping package com.apps.objectdetection uid=10036
D/PackageManager( 69): Scanning package com.apps.objectdetection
I/PackageManager( 69): Package com.apps.objectdetection codePath changed from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk; Retaining data and using new
I/PackageManager( 69): Unpacking native libraries for /data/app/com.apps.objectdetection-2.apk
D/dalvikvm( 69): GC_CONCURRENT freed 1266K, 48% free 4269K/8199K, external 4373K/5573K, paused 9ms+7ms
D/installd( 35): DexInv: --- BEGIN '/data/app/com.apps.objectdetection-2.apk' ---
D/dalvikvm( 333): DexOpt: load 64ms, verify+opt 376ms
D/installd( 35): DexInv: --- END '/data/app/com.apps.objectdetection-2.apk' (success) ---
W/PackageManager( 69): Code path for pkg : com.apps.objectdetection changing from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk
W/PackageManager( 69): Resource path for pkg : com.apps.objectdetection changing from /data/app/com.apps.objectdetection-1.apk to /data/app/com.apps.objectdetection-2.apk
D/PackageManager( 69): Activities: com.apps.objectdetection.ObjectDetection com.apps.objectdetection.TakePictureActivity com.apps.objectdetection.AnalyzeActivity
I/ActivityManager( 69): Force stopping package com.apps.objectdetection uid=10036
I/installd( 35): move /data/dalvik-cache/data@app@com.apps.objectdetection-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.apps.objectdetection-2.apk@classes.dex
D/PackageManager( 69): New package installed in /data/app/com.apps.objectdetection-2.apk
I/ActivityManager( 69): Force stopping package com.apps.objectdetection uid=10036
D/dalvikvm( 139): GC_EXPLICIT freed 75K, 52% free 2908K/5959K, external 4984K/5293K, paused 65ms
D/dalvikvm( 194): GC_EXPLICIT freed 292K, 52% free 2769K/5703K, external 1625K/2137K, paused 158ms
I/ActivityManager( 69): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=334 uid=10009 gids={}
W/RecognitionManagerService( 69): no available voice recognition services found
I/ActivityThread( 334): Pub com.svox.pico.providers.SettingsProvider: com.svox.pico.providers.SettingsProvider
D/dalvikvm( 69): GC_EXPLICIT freed 445K, 49% free 4242K/8199K, external 4373K/5573K, paused 80ms
I/installd( 35): unlink /data/dalvik-cache/data@app@com.apps.objectdetection-1.apk@classes.dex
D/AndroidRuntime( 240): Shutting down VM
D/dalvikvm( 240): GC_CONCURRENT freed 100K, 72% free 293K/1024K, external 0K/0K, paused 3ms+1ms
D/jdwp ( 240): adbd disconnected
I/AndroidRuntime( 240): NOTE: attach of thread 'Binder Thread #3' failed
D/AndroidRuntime( 347):
D/AndroidRuntime( 347): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime( 347): CheckJNI is ON
D/AndroidRuntime( 347): Calling main entry com.android.commands.am.Am
I/ActivityManager( 69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.apps.objectdetection/.ObjectDetection } from pid 347
I/ActivityManager( 69): Start proc com.apps.objectdetection for activity com.apps.objectdetection/.ObjectDetection: pid=355 uid=10036 gids={1006, 1015}
D/AndroidRuntime( 347): Shutting down VM
D/dalvikvm( 347): GC_CONCURRENT freed 101K, 69% free 318K/1024K, external 0K/0K, paused 2ms+1ms
D/jdwp ( 347): adbd disconnected
I/AndroidRuntime( 347): NOTE: attach of thread 'Binder Thread #3' failed
E/TAG ( 355): OD onCraete called!
D/CameraHardwareStub( 34): initHeapLocked: preview size=320x240
I/StagefrightPlayer( 34): setDataSource('/system/media/audio/ui/camera_click.ogg')
I/StagefrightPlayer( 34): setDataSource('/system/media/audio/ui/VideoRecord.ogg')
D/CameraHardwareStub( 34): initHeapLocked: preview size=320x240
I/ActivityManager( 69): Displayed com.apps.objectdetection/.ObjectDetection: +2s500ms (total +46s538ms)
I/ActivityManager( 69): Displayed com.android.launcher/com.android.launcher2.Launcher: +46s542ms
I/InputReader( 69): Device reconfigured: id=0x0, name=qwerty2, display size is now 480x800
I/InputManager-Callbacks( 69): No virtual keys found for device qwerty2.
I/ARMAssembler( 69): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x444ea520:0x444ea6d8] in 2041001 ns
I/ARMAssembler( 69): generated scanline__00000077:03515104_00001004_00000000 [ 65 ipp] (85 ins) at [0x444ea6e0:0x444ea834] in 701000 ns
I/ARMAssembler( 69): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x444ea838:0x444eaa00] in 642000 ns
E/SurfaceFlinger( 69): texture=11, using format 17, which is not supported by the GL
D/AudioSink( 34): bufferCount (4) is too small and increased to 12
E/SurfaceFlinger( 69): texture=7, using format 17, which is not supported by the GL
D/MediaScannerService( 226): start scanning volume external
V/MediaScanner( 226): pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@40528098
V/MediaScanner( 226): /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@40528098
D/MediaScanner( 226): prescan time: 81ms
D/MediaScanner( 226): scan time: 53ms
D/MediaScanner( 226): postscan time: 26ms
D/MediaScanner( 226): total time: 160ms
D/MediaScannerService( 226): done scanning volume external
E/SurfaceFlinger( 69): texture=7, using format 17, which is not supported by the GL
D/MediaScannerService( 226): start scanning volume external
D/MediaScanner( 226): prescan time: 88ms
D/MediaScanner( 226): scan time: 55ms
D/MediaScanner( 226): postscan time: 0ms
D/MediaScanner( 226): total time: 143ms
D/MediaScannerService( 226): done scanning volume external
I/InputDispatcher( 69): Application is not responding: Window{4074e398 com.apps.objectdetection/com.apps.objectdetection.ObjectDetection paused=false}. 5006.2ms since event, 5005.8ms since wait started
I/WindowManager( 69): Input event dispatching timed out sending to com.apps.objectdetection/com.apps.objectdetection.ObjectDetection
I/Process ( 69): Sending signal. PID: 355 SIG: 3
I/dalvikvm( 355): threadid=4: reacting to signal 3
I/dalvikvm( 355): Wrote stack traces to '/data/anr/traces.txt'
I/Process ( 69): Sending signal. PID: 69 SIG: 3
I/dalvikvm( 69): threadid=4: reacting to signal 3
I/dalvikvm( 69): Wrote stack traces to '/data/anr/traces.txt'
I/Process ( 69): Sending signal. PID: 131 SIG: 3
I/dalvikvm( 131): threadid=4: reacting to signal 3
I/dalvikvm( 131): Wrote stack traces to '/data/anr/traces.txt'
I/Process ( 69): Sending signal. PID: 135 SIG: 3
I/dalvikvm( 135): threadid=4: reacting to signal 3
I/dalvikvm( 135): Wrote stack traces to '/data/anr/traces.txt'
D/dalvikvm( 69): GC_EXPLICIT freed 479K, 48% free 4310K/8199K, external 4373K/5573K, paused 130ms
I/Process ( 69): Sending signal. PID: 226 SIG: 3
I/dalvikvm( 226): threadid=4: reacting to signal 3
I/dalvikvm( 226): Wrote stack traces to '/data/anr/traces.txt'
E/ActivityManager( 69): ANR in com.apps.objectdetection (com.apps.objectdetection/.ObjectDetection)
E/ActivityManager( 69): Reason: keyDispatchingTimedOut
E/ActivityManager( 69): Load: 2.06 / 1.16 / 0.44
E/ActivityManager( 69): CPU usage from 28574ms to 0ms ago:
E/ActivityManager( 69): 0.2% 41/adbd: 0% user + 0.2% kernel / faults: 64 minor
E/ActivityManager( 69): 0.1% 69/system_server: 0.1% user + 0% kernel / faults: 2 minor
E/ActivityManager( 69): 0% 131/com.android.phone: 0% user + 0% kernel / faults: 7 minor
E/ActivityManager( 69): 1.2% TOTAL: 0.4% user + 0.8% kernel + 0% softirq
E/ActivityManager( 69): CPU usage from 985ms to 1562ms later:
E/ActivityManager( 69): 14% 69/system_server: 7.1% user + 7.1% kernel / faults: 5 minor
E/ActivityManager( 69): 8.9% 103/InputDispatcher: 5.3% user + 3.5% kernel
E/ActivityManager( 69): 5.3% 70/HeapWorker: 1.7% user + 3.5% kernel
E/ActivityManager( 69): 1% 226/android.process.media: 1% user + 0% kernel
E/ActivityManager( 69): 1% 233/Binder Thread #: 1% user + 0% kernel
E/ActivityManager( 69): 17% TOTAL: 7% user + 10% kernel
I/InputDispatcher( 69): Dropping event because the pointer is not down.
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
I/dalvikvm( 69): Jit: resizing JitTable from 1024 to 2048
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
D/SntpClient( 69): request time failed: java.net.SocketException: Address family not supported by protocol
以下是traces.txt
----- pid 355 at 2012-02-05 23:53:50 -----
Cmd line: com.apps.objectdetection
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4001f1a8 self=0xce48
| sysTid=355 nice=0 sched=0/0 cgrp=default handle=-1345006528
| schedstat=( 726128083 1938849180 195 )
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:746)
at android.hardware.Camera.takePicture(Camera.java:710)
at com.apps.objectdetection.TakePictureActivity$2.onClick(TakePictureActivity.java:76)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #2" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40512758 self=0x1f8c40
| sysTid=367 nice=0 sched=0/0 cgrp=default handle=2557464
| schedstat=( 12951001 32247003 12 )
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x405111b0 self=0x117770
| sysTid=366 nice=0 sched=0/0 cgrp=default handle=1144624
| schedstat=( 18285003 36422003 16 )
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=6 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050eca8 self=0x116c00
| sysTid=365 nice=0 sched=0/0 cgrp=default handle=1143144
| schedstat=( 2489001 48433004 6 )
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050ebf8 self=0x116ac8
| sysTid=364 nice=0 sched=0/0 cgrp=default handle=580152
| schedstat=( 22429003 84657008 23 )
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x4050eb38 self=0x116fa8
| sysTid=363 nice=0 sched=0/0 cgrp=default handle=576032
| schedstat=( 11413002 30484003 8 )
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050ea90 self=0x8d848
| sysTid=358 nice=0 sched=0/0 cgrp=default handle=576384
| schedstat=( 723000 9093001 2 )
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x4050e9d8 self=0x1165f0
| sysTid=357 nice=0 sched=0/0 cgrp=default handle=969568
| schedstat=( 77777007 532447054 23 )
at dalvik.system.NativeStart.run(Native Method)
----- end 355 -----
答案 0 :(得分:1)
我在真实手机上部署应用后,此错误消失了。这似乎是模拟器的一个问题。
答案 1 :(得分:0)
尝试此代码..我使用此代码来消除VM关闭的问题。您可以设置解码方法来设置您存储或使用的图像大小。试试这个,我可以帮到这里,否则抱歉我没有比这更多的帮助...
public class UploadImage extends Activity {
Bitmap _bitmap;
File imgFile;
Uri selectedImageUri;
private String filePath;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.uploadimage);
imgFile = new File(path);
// String name=imgFile.
if (imgFile.exists()) {
selectedImageUri =Upload.selectedUri;
Log.d("uriiiiiiiiiiiii", "uriiiiiiiiiiiiiiiii"+selectedImageUri);
filePath = null;
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
Log.d("uriiiiiiiiiiiii", "uriiiiiiiiiiiiiiiii"+filemanagerstring);
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
Log.d("uriiiiiiiiiiiii", "uriiiiiiiiiiiiiiiii"+selectedImagePath);
if (selectedImagePath != null) {
filePath = selectedImagePath;
Log.d("uriiiiiiiiiiiii", "uriiiiiiiiiiiiiiiii"+selectedImagePath);
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
Log.d("uriiiiiiiiiiiii", "uriiiiiiiiiiiiiiiii"+filePath);
} else {
Toast.makeText(getApplicationContext(),"Unknown path", Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
_bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
_bitmap = BitmapFactory.decodeFile(filePath, o2);
int width = _bitmap.getWidth();
int height = _bitmap.getHeight();
//new width / heigth
int newWidth = 300;
int newHeight = 460;
// calculate the scale
float scaleWidth = (float) newWidth / width;
float scaleHeight = (float) newHeight / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// matrix.preRotate(90);//
// matrix.postRotate(270);
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap and set it back
Bitmap resizedBitmap = Bitmap.createBitmap(_bitmap, 0, 0,width, height, matrix, true);
_bitmap = resizedBitmap;
Set this bitmap where you want to use..
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}