简体   繁体   中英

can not getting image from camera in android 6

I startActivityForResult from one of my viewpager fragment to get image from camera but it is ok in android version 5 and less but in android version 6 i have problem.

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        Toast.makeText(getActivity(), "we are here Resultcode: "+requestCode, Toast.LENGTH_SHORT).show();
            if(resultCode!=-1 && data==null)
                return;
        Toast.makeText(getActivity(),"Getting readdy for image",Toast.LENGTH_LONG).show();
            switch(requestCode) {
                case PICK_IMAGE_ID:
                    Bitmap bitmap = ImagePicker.getImageFromResult(getActivity(), resultCode, data);
               /* Prescription_img=(ImageView)getActivity().findViewById(R.id.prescription);
                Prescription_img.setImageBitmap(bitmap);*/
                    Toast.makeText(getActivity(),"We are inside ",Toast.LENGTH_LONG).show();
                    File file= null;
                    try {
                        file = savebitmap(bitmap);
                        Log.d("file:",file.getName() + "  |is the file exist: "+file.getAbsolutePath());
                        f=file;
                        //control fragment menu to show step to get prescription from drugstore
                        Toast.makeText(getActivity(),"gott",Toast.LENGTH_LONG).show();

                        Log.i("getImage", "image gotten successfully");
                        Bundle args=new Bundle();
                        args.putString("image",f.getAbsolutePath());
                        _step2_fragmentconfirm_pic confirmPicFragment=new _step2_fragmentconfirm_pic();
                        confirmPicFragment.setArguments(args);
                        Toast.makeText(getActivity(),"getting image ready",Toast.LENGTH_LONG).show();
                        FragmentTransaction transaction=getFragmentManager().beginTransaction();
                        transaction.replace(R.id.root_menu_fragment,confirmPicFragment);
                        transaction.commit();


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    super.onActivityResult(requestCode, resultCode, data);
                    break;
            }
            Log.d("ImageGallary:",Integer.toString(requestCode));

        }

and the getImageFromResult method in ImagePicker class is here:

public static Bitmap getImageFromResult(Context context, int resultCode,
                                        Intent imageReturnedIntent) {
    Log.d(TAG, "getImageFromResult, resultCode: " + resultCode);
    Bitmap bm = null;
    File imageFile = getTempFile(context);
    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage;
        boolean isCamera = (imageReturnedIntent == null ||
                imageReturnedIntent.getData() == null  ||
                imageReturnedIntent.getData().toString().contains(imageFile.toString()));
        if (isCamera) {     /** CAMERA **/
            selectedImage = Uri.fromFile(imageFile);
        } else {            /** ALBUM **/
            selectedImage = imageReturnedIntent.getData();
        }
        Log.d(TAG, "selectedImage: " + selectedImage);

        bm = getImageResized(context, selectedImage);
        int rotation = getRotation(context, selectedImage, isCamera);
        bm = rotate(bm, rotation);
    }
    return bm;

}

I traced the app and find out that in try block when i want to get file from savebitmap it shot me to catch section on this line:

file = savebitmap(bitmap);

and also this is my full imagepicker class:

https://gist.github.com/anonymous/18a0752722e877ac6b15b7a7be16f573

I tried to get error message in catch block by this command Log.i("Error",e.getMessage()); and get this message:

01-18 10:25:21.462 269-618/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.462 269-15025/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 185208591, value : 202116107
01-18 10:25:21.502 269-8865/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.502 269-15029/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 319819276, value : 319818764
01-18 10:25:21.532 269-1526/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.542 269-15033/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 286330641, value : 269553937
01-18 10:25:21.552 269-24674/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.562 269-15038/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 286330641, value : 269553937
01-18 10:25:21.582 269-269/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.592 269-15042/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 54424, value : 0
01-18 10:25:21.762 269-619/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.772 269-15057/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 319819276, value : 319818764
01-18 10:25:21.802 269-618/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.812 269-15061/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 151587081, value : 151587081
01-18 10:25:21.872 269-14940/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.922 269-8865/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.932 269-15075/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 319819276, value : 319818764
01-18 10:25:21.972 269-269/? V/AwesomePlayer: startAudioPlayer_l, sendErrorNotification (0)
01-18 10:25:21.982 269-15079/? E/AudioPlayer: >>> setAudioEffect Error mAudioFormat : 1, event : 319819276, value : 319818764
01-18 10:25:35.842 216-216/? E/lowmemorykiller: Error writing /proc/8061/oom_score_adj; errno=22
01-18 10:25:35.862 216-216/? E/lowmemorykiller: Error writing /proc/8061/oom_score_adj; errno=22

In android 6 you need to do runtime permisions

https://developer.android.com/training/permissions/requesting.html

Example

if (checkSelfPermission(Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {

    requestPermissions(new String[]{Manifest.permission.CAMERA , Manifest.permission.WRITE_EXTERNAL_STORAGE},
        MY_REQUEST_CODE);
}

and in result activity (make sure you don't disturb the current one )

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions,      int[] grantResults) {
    if (requestCode == MY_REQUEST__CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Now user should be able to use camera
        }
    else {
         // Your app will not have this permission. Turn off all functions 
         // that require this permission or it will force close like your 
         // original question
         }
    }
}

this code should run before going to the camera , the user will grant you permision to use the camera and save an image to external if he won't give you don't do it and send a msg that you need permision to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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