3

我编写了一个应用程序,它应该拍照然后在屏幕上显示以进行修改。在 eclipse 模拟器上尝试时,相机无法工作,所以我在 Galaxy Nexus 智能手机上尝试。

然而,当在我的 SP 上运行它时,应用程序会崩溃说it unfortunally stopped working

执行应用程序时,这正是发生的情况:

  1. 我点击相机按钮,相机界面被打开
  2. 拍照后,我可以选择丢弃或打开它
  3. 如果我单击放弃应用程序将恢复正常使用
  4. 如果我点击打开应用程序崩溃如上所述

我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
4

3 回答 3

2

听起来和看起来您不允许系统自行更新已创建新媒体文件。这就是你的方法失败的原因。您可以手动创建图像文件路径,以便在文件树上拥有图像位置,也可以调用媒体服务来运行更新。我总是创建自己的文件路径,因为旧手机需要更长时间才能使用媒体服务进行更新,因此在这种情况下您的方法会失败。

于 2012-09-24T19:48:46.790 回答
2

显然,这次崩溃是由于三星的一个已知问题:似乎您需要Uri在调用相机意图之前创建一个,这样在运行该onActivityResult方法时,内容提供者将已经分配了保存资源的位置。

有关更多信息,请检查以下内容:

还有更多通过谷歌搜索......

PS我会尽快发布对我有用的解决方案。

于 2012-09-25T07:07:53.747 回答
0

我知道现在回答它已经很晚了,但我尽快找到了我回答的答案,所以请至少检查一下。

AndroidCameraUtil可能是每个设备的简单易用的解决方案,下面是您可以与库一起使用的代码片段

 private void setupCameraIntentHelper() {
    mCameraIntentHelper = new CameraIntentHelper(this, new CameraIntentHelperCallback() {
        @Override
        public void onPhotoUriFound(Date dateCameraIntentStarted, Uri photoUri, int rotateXDegrees) {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_found) + photoUri.toString());

            Bitmap photo = BitmapHelper.readBitmap(CameraIntentActivity.this, photoUri);
            if (photo != null) {
                photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);
                ImageView imageView = (ImageView) findViewById(de.ecotastic.android.camerautil.sample.R.id.activity_camera_intent_image_view);
                imageView.setImageBitmap(photo);
            }
        }

        @Override
        public void deletePhotoWithUri(Uri photoUri) {
            BitmapHelper.deleteImageWithUriIfExists(photoUri, CameraIntentActivity.this);
        }

        @Override
        public void onSdCardNotMounted() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sd_card_not_mounted), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCanceled() {
            Toast.makeText(getApplicationContext(), getString(R.string.warning_camera_intent_canceled), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCouldNotTakePhoto() {
            Toast.makeText(getApplicationContext(), getString(R.string.error_could_not_take_photo), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPhotoUriNotFound() {
            messageView.setText(getString(R.string.activity_camera_intent_photo_uri_not_found));
        }

        @Override
        public void logException(Exception e) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sth_went_wrong), Toast.LENGTH_LONG).show();
            Log.d(getClass().getName(), e.getMessage());
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    mCameraIntentHelper.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mCameraIntentHelper.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mCameraIntentHelper.onActivityResult(requestCode, resultCode, intent);
}
}
于 2015-12-07T04:17:02.370 回答