0

startActivityForResult从我的视图页面片段之一从相机获取图像,但在 android 版本 5 及更低版本中可以,但在 android 版本 6 中我有问题。

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

        }

ImagePicker 类中的 getImageFromResult 方法在这里:

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;

}

我跟踪了应用程序,发现在尝试块中,当我想从 savebitmap 获取文件时,它让我捕捉到了这一行的部分:

file = savebitmap(bitmap);

这也是我完整的图像选择器类:

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

我试图通过这个命令 Log.i("Error",e.getMessage()); 在 catch 块中获取错误消息 并收到此消息:

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
4

1 回答 1

1

在 android 6 中,您需要执行运行时权限

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

例子

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

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

并在结果活动中(确保您不打扰当前活动)

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

这段代码应该在去相机之前运行,如果他不给你不这样做,用户将授予你使用相机的权限并将图像保存到外部,并发送一个你需要权限的消息。

于 2017-01-18T07:55:42.063 回答