调用有抛出IOException异常

问题描述:

这是我的代码的方法:调用有抛出IOException异常

public class CameraFragment extends Fragment{ 

String mCurrentPhotoPath = ""; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    try { 
     dispatchTakePictureIntent(); // Here I try to call this method 
    } catch (IOException dfg) { 
     Toast.makeText(getContext(), "ERROR!", Toast.LENGTH_LONG).show(); 
    } 
    //dispatchTakePictureIntent(); //I can't call this like I did here 

    return inflater.inflate(R.layout.fragment_camera, container, false); 
} 

ImageView SkimmedImageImg; 
@Override 
public void onViewCreated(View view, Bundle savedInstanceState){ 
    super.onViewCreated(view, savedInstanceState); 
    SkimmedImageImg = (ImageView)view.findViewById(R.id.SkimmedImg); 
} 

static final int REQUEST_IMAGE_CAPTURE = 1; 

private void dispatchTakePictureIntent() throws IOException{ 
    ..CODE.. 
    photo = createImageFile(); 
} 

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

private File createImageFile() throws IOException{ 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_DCIM), "Camera"); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

} 

我怎样才能解决这个问题? 我不能从每个函数中删除“throws IOException”,因为我必须调用“createImageFile()”,并且在没有“throws IOException”的情况下这不起作用。

有些想法? 谢谢!

+0

你有没有了解异常处理什么?特别是'try'或'catch'? –

+1

您之前的回答正确地使用了它......为什么你没有使用该代码? http://*.com/a/42330491/2308683 –

+0

“我该如何解决这个问题?”什么问题?具体的错误是什么? – Alex

在你的代码...

//dispatchTakePictureIntent(); //I can't call this like I did here 

权......你不能没有try-catch -ing调用它。该方法throws


同样,在你的代码

try { 
    dispatchTakePictureIntent(); // Here I try to call this method 
} catch (IOException dfg) { 
    Toast.makeText(getContext(), "ERROR!", Toast.LENGTH_LONG).show(); 
} 

你调用的方法精细。您“发现”throws正在宣布的异常。

如果你想实际“看到”错误,不要敬酒。这是一个糟糕的用户界面设计,需要随机弹出。

使用日志(和名称dfg更好)

​​