拍照后没有调用onActivityResult

时间:2014-01-15 13:44:20

标签: java android android-camera

如何知道该主题有详细记录,我已经阅读了很多关于该问题,但我仍然有以下问题:当我用我的应用程序拍照并点击“验证”按钮时,什么也没发生。我正在做的事情:不仅传递给onActivityReult函数缩略图,还传递摄像机拍摄的“整个”照片。

这是为“拍照”按钮定义的监听器:

@Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraTest");
            mediaStorageDir.mkdir(); // make sure you got this folder
            Log.i("Report",mediaStorageDir.toString());
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");

            try
            {
                //create directories and the file
                mediaFile.getParentFile().mkdirs();
                mediaFile.createNewFile();
            } catch (IOException e) { 
                Log.e("Report", "create error for file "+mediaFile);
                e.printStackTrace();
            }
            mFileUri = Uri.fromFile(mediaFile);
            Log.i("Report","Uri: "+mFileUri);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);// this line causes issue - onActivityResult not called...
            startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});

这里是onActivityResult方法......永远不会被调用(并且没有在onClickListener方法中声明):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("Report", "1");
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            try {
                String[] projection = {
                        MediaStore.Images.Thumbnails._ID, // The columns we want
                        MediaStore.Images.Thumbnails.IMAGE_ID,
                        MediaStore.Images.Thumbnails.KIND,
                        MediaStore.Images.Thumbnails.DATA };
                String selection = MediaStore.Images.Thumbnails.KIND + "=" + 
                        MediaStore.Images.Thumbnails.MINI_KIND;
                String sort = MediaStore.Images.Thumbnails._ID + " DESC";
                Cursor myCursor = this.managedQuery(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        projection, selection, null, sort);
                Log.d("Report", "3");
                long imageId = 0l;
                long thumbnailImageId = 0l;
                String thumbnailPath = "";

                try {
                    myCursor.moveToFirst();
                    imageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
                    thumbnailImageId = myCursor
                            .getLong(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
                    thumbnailPath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
                } finally {
                    myCursor.close();
                }

                String[] largeFileProjection = {
                        MediaStore.Images.ImageColumns._ID,
                        MediaStore.Images.ImageColumns.DATA };

                String largeFileSort = MediaStore.Images.ImageColumns._ID
                        + " DESC";
                myCursor = this.managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        largeFileProjection, null, null, largeFileSort);
                String largeImagePath = "";

                try {
                    myCursor.moveToFirst();

                    // This will actually give yo uthe file path location of the
                    // image.
                    largeImagePath = myCursor
                            .getString(myCursor
                                    .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
                    mImageCaptureUri = Uri.fromFile(new File(
                            largeImagePath));

                } finally {
                    // myCursor.close();
                }
                Uri uriLargeImage = Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        String.valueOf(imageId));
                Uri uriThumbnailImage = Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        String.valueOf(thumbnailImageId));

                Bitmap thumbnail = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriThumbnailImage);
                Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriLargeImage);

但是,如标题中所述,onActivityResult未被调用。你能找出原因吗?因为我已经尝试了几乎所有我在该主题上找到的东西,但我应该错过了一些东西。

谢谢!

3 个答案:

答案 0 :(得分:1)

检查您是否在AndroidManifest.xml

中声明了正确的权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

并确保您要写入的文件存在:
puc_img = new File(photo,"Puc_Img.jpg");

下面添加以下内容
try
{
    //create directories and the file
    puc_file.getParentFile().mkdirs();
    puc_file.createNewFile();
} catch (IOException e) { }    

答案 1 :(得分:0)

您的代码中不清楚,但您可以在onActivityResult内声明onClickListener。如果这是真的你需要移动它。 看看这个答案:

OnActivityResult ()

答案 2 :(得分:0)

好的,为了解决这个问题,我使用了一些&#34;技巧&#34;详细信息如下:

@Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraTest");
            mediaStorageDir.mkdir(); // make sure you got this folder
            Log.i("Report",mediaStorageDir.toString());
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");

            try
            {
                //create directories and the file
                mediaFile.getParentFile().mkdirs();
                mediaFile.createNewFile();
            } catch (IOException e) { 
                Log.e("Report", "create error for file "+mediaFile);
                e.printStackTrace();
            }
            tmpFilePath = mediaFile.getPath();
            mFileUri = Uri.fromFile(mediaFile);
            Log.i("Report","Uri: "+mFileUri);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
            startActivityForResult(intent, CAMERA_PIC_REQUEST);
        }

    });

那么,我正在恢复存储在tmpFilePath中的文件,在onActivityResult函数中使用该方法:

Bitmap image = BitmapFactory.decodeFile(this.tmpFilePath);

而且......工作正常。将文件发送到WS时仍然存在一些问题但是这个问题已经解决了。谢谢你的帮助,你让我走上了轨道:)