简体   繁体   English

拍照后未调用onActivityResult

[英]onActivityResult not called after taking picture

How know that subject is well documented and I have read a lot on that issue, but I still have the following problem: when I take a picture with my app and click on "validate" button, nothing occur. 我知道该主题的文献记录很丰富,因此我已经阅读了很多有关该问题的文章,但是仍然存在以下问题:当我使用我的应用拍照并单击“验证”按钮时,什么也没有发生。 The aime of what I am doing: passing to onActivityReult function not only the thumbnail, but the "whole" picture taken by the camera. 我正在做的动画:不仅向缩略图传递onActivityReult函数,而且传递相机拍摄的“整个”照片。

Here is the listener as defined for the "take a picture" button: 这是为“拍照”按钮定义的侦听器:

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

and here is the onActivityResult method... that is never called (and that is not declared in the onClickListener method): 这是永远不会调用的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);

But, as said in the title, onActivityResult is not called. 但是,正如标题中所述,不会调用onActivityResult。 Could you please find out why? 你能找出原因吗? Because I have tried almost everything I have found on that subject but I should have missed something. 因为我已经尝试了在该主题上发现的几乎所有东西,但是我应该错过一些东西。

Thanks ! 谢谢 !

check if you have declared the right permissions in the AndroidManifest.xml 检查您是否在AndroidManifest.xml声明了正确的权限

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

and be sure the file you want to write into exists: 并确保要写入的文件存在:
add this below puc_img = new File(photo,"Puc_Img.jpg"); puc_img = new File(photo,"Puc_Img.jpg");下添加此内容puc_img = new File(photo,"Puc_Img.jpg");

try
{
    //create directories and the file
    puc_file.getParentFile().mkdirs();
    puc_file.createNewFile();
} catch (IOException e) { }    

It's not clear from your code but you could be declaring onActivityResult within your onClickListener . 这不是从你的代码清楚,但你可能会宣布onActivityResult你的内onClickListener If that's true you need to move it. 如果是这样,则需要移动它。 Take a look at this answer: 看一下这个答案:

OnActivityResult () OnActivityResult()

Ok, to solve that issue, I used some "trick" detailed here: 好的,为了解决这个问题,我在这里使用了一些“技巧”:

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

    });

So then, I am recovering the file stored in the tmpFilePath, by using that method in the onActivityResult function: 因此,通过使用onActivityResult函数中的方法,我将恢复存储在tmpFilePath中的文件:

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

And... It's working fine. 而且...工作正常。 I still have some issues when sending the file to the WS but that very question have been solved by that piece of code. 将文件发送到WS时,我仍然遇到一些问题,但是那个代码部分已经解决了这个问题。 Thanks for the help, you put me on the track :) 感谢您的帮助,您使我如愿以偿:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM