拍照后onActivityResult未被调用

3
我知道这个主题已经有很好的文档记录,并且我已经阅读了很多相关问题,但我仍然遇到了以下问题:当我使用我的应用程序拍照并点击“验证”按钮时,什么都没有发生。我的目标是将不仅缩略图,而且由相机拍摄的整张图片传递给onActivityResult函数。

以下是为“拍照”按钮定义的侦听器:

@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没有被调用。你能找出原因吗?因为我已经尝试了几乎所有相关的东西,但我可能错过了什么。

谢谢!


你确定它没有被调用吗?你用调试器检查过了吗? - Aron Lorincz
onActivityResult方法的第一行是一个打印语句,但在logcat中没有任何输出。 - Derbie
1
请从您的onActivityResult()函数中删除super.onActivityResult(requestCode, resultCode, data);(第二行)。 - Ahmad Raza
是的。使用调试器来检查它。在onActivityResult的第一行处设置断点,看看是否触发了它。(在Eclipse中按Ctrl+Shift+B或Cmd+Shift+B(在mac上)以切换行断点。) - Aron Lorincz
问题是当我点击相机的“验证”按钮时,什么也没有发生,这也让我认为onActivityResult方法没有被触发。无论如何,我现在会立即执行您建议的检查。 - Derbie
显示剩余3条评论
3个回答

2

请检查你在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) { }    

我确实有这些行 - Derbie
据我所知,在拍照之前必须存在该文件,因此请尝试上述代码 :) - 我已进一步更新它,以提供一个现有的目录来创建该文件。 - bricklore
这是你代码的一个补充。请将其放在 puc_img = new File(photo,"Puc_Img.jpg"); 下面。 - bricklore
Rrraaaah。好的。这就是我所做的。由于我只使用了photo var,所以我删除了所有puc_img出现的地方。我在基础问题中相应地编辑了代码。但是现在,我仍然无法触发onActivityResult方法。我做错了什么? - Derbie
嗯,这对我来说似乎非常奇怪。尝试在 super.onActivityResult(...) 之前放置一个记录器,看看是否会有任何变化,但老实说,我不认为会有变化 :( - bricklore
显示剩余6条评论

0

好的,为了解决这个问题,我使用了这里详细介绍的一些“技巧”:

@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);
        }

    });

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

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

还有...它运行得很好。当我将文件发送到WS时仍然存在一些问题,但是那个代码片段解决了这个问题。感谢您的帮助,您让我找到了解决方法 :)


0

从你的代码中并不清楚,但你可能在onClickListener内声明了onActivityResult。如果是这样,你需要将它移动到外面。

看一下这个答案:

OnActivityResult()


不,onActivityResult方法没有在onClickListener中声明 :) - Derbie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接