我编写了一个应用程序,它应该拍照然后在屏幕上显示以进行修改。在 eclipse 模拟器上尝试时,相机无法工作,所以我在 Galaxy Nexus 智能手机上尝试。
然而,当在我的 SP 上运行它时,应用程序会崩溃说it unfortunally stopped working
。
执行应用程序时,这正是发生的情况:
- 我点击相机按钮,相机界面被打开
- 拍照后,我可以选择丢弃或打开它
- 如果我单击放弃应用程序将恢复正常使用
- 如果我点击打开应用程序崩溃如上所述
我google了一下,发现你需要使用硬件设备的权限检查here,所以我创建了文件/etc/udev/rules.d/51-android.rules
,这是它的内容:
SUBSYSTEM=="USB", ATTR{IDVENDOR}=="18d1", MODE="0666, "GROUP="plugdev" SUBSYSTEM=="USB", ATTR{IDVENDOR}=="04e8", MODE="0666, "GROUP="plugdev" SUBSYSTEM=="USB", ATTR{IDVENDOR}=="0bb4", MODE="0666, "GROUP="plugdev"
但是我仍然无法使用相机。
以下是我在清单文件中声明的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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" />
这是我用来启动相机意图的代码:
//create new Intent
Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
//get something back from the activity we are starting
startActivityForResult( cameraIntent, CAMERA_PICTURE_REQUEST );
这是处理结果的代码:
public void onActivityResult( int requestCode, int resultCode, Intent imageReturnedIntent )
{
if( resultCode == RESULT_OK )
{
if( requestCode == GALLERY_PICTURE_REQUEST )
{
selectedImageUri = imageReturnedIntent.getData();
Log.d( TAG, selectedImageUri );
Intent intent = new Intent( DVAHLUI_SuperviseActivity.this, DVAHLUI_SelectImageContentActivity.class );
intent.setData( selectedImageUri );
startActivity( intent );
}
if( requestCode == CAMERA_PICTURE_REQUEST )
{
selectedImageUri = imageReturnedIntent.getData();
Log.d( TAG, selectedImageUri );
Intent intent = new Intent( DVAHLUI_SuperviseActivity.this, DVAHLUI_SelectImageContentActivity.class );
intent.setData( selectedImageUri );
startActivity( intent );
}
}
}
这是导致 Java Null 指针异常的 getPath() 函数:
public String getPath( Uri uri )
{
String[] filePathColumn = { android.provider.MediaStore.Images.Media.DATA };
LINE 343 --> Cursor cursor = getContentResolver().query( uri, filePathColumn, null, null, null );
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndexOrThrow( filePathColumn[0] );
String filePath = cursor.getString( columnIndex );
cursor.close();
return filePath;
}
你能告诉我出了什么问题吗?
FOGOT 发布 LOGCAT:
E/AndroidRuntime(27859): FATAL EXCEPTION: main
E/AndroidRuntime(27859): java.lang.RuntimeException: Failure delivering result ResultInfo{who=supervise, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.DVA_HLUI/com.DVA_HLUI.DVAHLUI_TabModeActivity}: java.lang.NullPointerException
E/AndroidRuntime(27859): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
E/AndroidRuntime(27859): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
E/AndroidRuntime(27859): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
E/AndroidRuntime(27859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
E/AndroidRuntime(27859): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27859): at android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(27859): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(27859): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27859): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(27859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(27859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(27859): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(27859): Caused by: java.lang.NullPointerException
E/AndroidRuntime(27859): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1094)
E/AndroidRuntime(27859): at android.content.ContentResolver.query(ContentResolver.java:354)
E/AndroidRuntime(27859): at android.content.ContentResolver.query(ContentResolver.java:313)
E/AndroidRuntime(27859): at com.DVA_HLUI.DVAHLUI_SuperviseActivity.getPath(DVAHLUI_SuperviseActivity.java:343)
E/AndroidRuntime(27859): at com.DVA_HLUI.DVAHLUI_SuperviseActivity.onActivityResult(DVAHLUI_SuperviseActivity.java:312)
E/AndroidRuntime(27859): at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:122)
E/AndroidRuntime(27859): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
E/AndroidRuntime(27859): ... 11 more