我能夠從圖庫中拍攝圖片,但是發現攝像頭在片段中捕獲問題。 在拍攝onActivityResult圖片後有時會調用它,並在調用時會給出一些未找到的異常文件。在android片段中拍攝來自攝像頭的圖片不起作用
我的代碼是
if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) {
if(flag==0){
try{
String URI = getImageURI();
String imageName = URI.substring(URI.lastIndexOf("/")+1);
FileInputStream fis = mContext.openFileInput(imageName);
Bitmap photo = BitmapFactory.decodeStream(fis);
Matrix matrix = new Matrix();
matrix.preRotate(90);
photo = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
}
catch(Exception e){
Log.e("Error - ",e.getMessage());
}
}
}
public void takePictureFromCamera(){
File style = new File(Environment.getExternalStorageDirectory(),"style");
if(!style.exists()){style.mkdir();}
String d = System.currentTimeMillis()+"";
File f = new File(style, d+"style.jpg");
absPath = f.getAbsolutePath();
savePref(absPath);
cameraImagePath = Uri.fromFile(f);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
}
getActivity().startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
1.對於「現在發現文件錯誤」在您正在測試的設備上,即Nexus不支持內存卡,因此無法使用** getExternalStorageDirectory **方法獲取,或者第二個原因是您沒有在清單中添加寫入權限, 2. for ** onActivtyResult未被調用**原因是因爲它調用了它的超級活動的onActivityResult,因此您可能需要從那裏調用子片段的方法。 – swiftBoy
Thannks爲你的答案,我正在三星Galaxy S4上測試它,並在manifest.And中添加權限。我必須從onActivityResult調用你提及的那個調用子片段方法的方法。 – Ravi