【问题标题】:call camera, take picture but fails on 'save'调用相机,拍照但“保存”失败
【发布时间】:2015-04-26 20:53:05
【问题描述】:

我下面的代码是一个带有 2 个按钮和一个图像视图的活动。我触发相机意图,相机打开,我拍照并在本机相机应用程序中选择“确认按钮”。

点击“确认”/“保存”时,相机崩溃,我的应用代码在onActivityResult 重新输入,我看到RESULT_OK 为-1。

有人知道可能是什么问题,或者我确实可以如何跟踪问题吗?我可以在此处提交的任何日志,除了相机应用程序屏幕中的错误,我看不到任何错误。

可能是存储权限吗,我假设相机正试图在“保存”点进行存储,那是它崩溃的时候。

public class NewUser_Activity extends Activity {


        /** The Constant PICK_IMAGE. */
        private static final int PICK_IMAGE = 0;

        /** The Constant PICK_IMAGE_FROM_GALLERY. */
        private static final int PICK_IMAGE_FROM_GALLERY = 1;

        /** The btn cancel. */
        private Button btnPhotoCamera,btnPhotoGallery,btnCancel;

        /** The img view. */
        private ImageView imgView;

        /** The u. */
        private Uri u;

        /* (non-Javadoc)
         * @see android.app.Activity#onCreate(android.os.Bundle)
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_newuser);

            imgView=(ImageView)findViewById(R.id.imgDisplayImage);
            btnPhotoCamera=(Button)findViewById(R.id.btnPhotoCamera);
            btnPhotoGallery=(Button)findViewById(R.id.btnPhotoGallery);
            btnCancel=(Button)findViewById(R.id.btnCancel);

            btnPhotoCamera.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    Intent camera=new Intent();
                    camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    camera.putExtra("crop", "true");

                    File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

                    u = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
                    camera.putExtra(MediaStore.EXTRA_OUTPUT, u);
                    startActivityForResult(camera, PICK_IMAGE);
                }
            });

    /* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode==RESULT_OK )
        {
            if(requestCode == PICK_IMAGE) {

                InputStream is=null;
                try {
                    is = this.getContentResolver().openInputStream(u);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                Bitmap bmp= BitmapFactory.decodeStream(is);
                imgView.setImageBitmap(bmp);
                Log.i("Inside", "PICK_IMAGE");
            }

            if (requestCode == PICK_IMAGE_FROM_GALLERY) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                Log.d("data",filePathColumn[0]);
                Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                imgView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                Log.i("Inside", "PICK_IMAGE_FROM_GALLERY");
            }
        }
    }
}

LogCat 在相机故障后重新输入代码的位置...

04-27 11:30:59.541    3561-3561/com.etsy.android.sample I/art﹕ Late-enabling -Xcheck:jni
04-27 11:30:59.590    3561-3561/com.etsy.android.sample W/ActivityThread﹕ Application com.etsy.android.sample is waiting for the debugger on port 8100...
04-27 11:30:59.590    3561-3561/com.etsy.android.sample I/System.out﹕ Sending WAIT chunk
04-27 11:30:59.659    3561-3572/com.etsy.android.sample I/art﹕ Debugger is active
04-27 11:30:59.791    3561-3561/com.etsy.android.sample I/System.out﹕ Debugger has connected
04-27 11:30:59.791    3561-3561/com.etsy.android.sample I/System.out﹕ waiting for debugger to settle...
04-27 11:31:02.794    3561-3561/com.etsy.android.sample I/System.out﹕ debugger has settled (1464)
04-27 11:31:02.850    3561-3941/com.etsy.android.sample D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-27 11:31:02.864    3561-3561/com.etsy.android.sample D/Atlas﹕ Validating map...
04-27 11:31:02.893    3561-3941/com.etsy.android.sample I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: xxxx: 01/14/15, xxxx, Ixxxdc
04-27 11:31:02.894    3561-3941/com.etsy.android.sample I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-27 11:31:02.924    3561-3941/com.etsy.android.sample D/OpenGLRenderer﹕ Enabling debug mode 0
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method void com.etsy.android.grid.ExtendableListView.rememberSyncState() would have incorrectly overridden the package-private method in android.widget.AdapterView
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method void com.etsy.android.grid.ExtendableListView.fillGap(boolean) would have incorrectly overridden the package-private method in android.widget.AbsListView
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method int com.etsy.android.grid.ExtendableListView.getFooterViewsCount() would have incorrectly overridden the package-private method in android.widget.AbsListView
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method int com.etsy.android.grid.ExtendableListView.getHeaderViewsCount() would have incorrectly overridden the package-private method in android.widget.AbsListView
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method void com.etsy.android.grid.ExtendableListView.invokeOnItemScrollListener() would have incorrectly overridden the package-private method in android.widget.AbsListView
04-27 11:31:07.794    3561-3561/com.etsy.android.sample W/art﹕ Before Android 4.1, method void com.etsy.android.grid.ExtendableListView.reportScrollStateChange(int) would have incorrectly overridden the package-private method in android.widget.AbsListView
04-27 11:31:07.801    3561-3561/com.etsy.android.sample D/StaggeredGridActivityFragment﹕ onScroll firstVisibleItem:0 visibleItemCount:0 totalItemCount:0
04-27 11:31:07.801    3561-3561/com.etsy.android.sample D/StaggeredGridActivityFragment﹕ onScroll lastInScreen - so load more
04-27 11:31:07.891    3561-3561/com.etsy.android.sample D/TilesAdapter﹕ getPositionRatio:0 ratio:1.4147916247938617
...
04-27 11:31:07.947    3561-3561/com.etsy.android.sample D/TilesAdapter﹕ getView position:2 h:1.4237136790554512
04-27 11:31:07.963    3561-3561/com.etsy.android.sample D/StaggeredGridActivityFragment﹕ onScroll firstVisibleItem:0 visibleItemCount:3 totalItemCount:3
...
04-27 11:31:10.209    3561-3561/com.etsy.android.sample D/StaggeredGridActivityFragment﹕ onScroll firstVisibleItem:0 visibleItemCount:3 totalItemCount:3

权限

>     <uses-permission android:name="android.permission.INTERNET" />
>     <uses-permission android:name="android.permission.CAMERA" />
>     <uses-permission android:name="android.permission.RECORD_VIDEO" />
>     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
>     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

【问题讨论】:

  • 我对这个问题有点困惑,因为 RESULT_OK 是 -1,所以看起来相机应用程序没有崩溃,而是告诉你它结果正确。 logcat 有什么东西吗?
  • logcat 中我看不到任何内容。相机确实崩溃了-'相机已停止工作'。相机关闭,我们返回我的代码(我称之为相机意图),result_ok = -1

标签: android android-intent camera


【解决方案1】:

【讨论】:

  • 您是否使用其他设备运行您的代码?根据我的经验,相机意图在所有制造商和 android 版本的所有 android 设备上的实施不一致。所以很难确定问题所在。因此,我在 github 上分享了我的解决方案以及经过测试的设备列表。随意尝试一下。
  • 我发现问题就在那儿,以为我更新了这个线程,可能是错误地更新了另一个...... - camera.putExtra("crop", "true");让它崩溃。那是怎么回事?这是和 dev 的一个主要缺点,并且会认为它会被覆盖。我现在必须找到一种行之有效的裁剪方法
【解决方案2】:

是这样的; camera.putExtra("crop", "true");

Android Studio 真的很烦人,它不会将我的代码从应用程序跟踪到相机应用程序,所以你看不到它,这是猜测工作。

如果这不起作用,我该如何裁剪?

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多