我来自我的 viewpager 片段中的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;
}
我跟踪应用程序并发现在try块中,当我想从savebitmap获取文件时,它会让我抓住这一行的部分:
file = savebitmap(bitmap);
这也是我的完整imagepicker类:
https://gist.github.com/anonymous/18a0752722e877ac6b15b7a7be16f573
我试图通过此命令在catch块中获取错误消息 Log.i( “错误”,e.getMessage()); 并收到此消息:
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
答案 0 :(得分:1)
在android 6中,你需要做运行时的permisions
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
}
}
}
此代码应在进入相机之前运行,用户将授予您使用相机的权限,并将图像保存到外部,如果他不会让您不这样做并发送您需要保留的消息这样做。