调用抛出IOException的方法

时间:2017-02-22 21:56:10

标签: android 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;
}

}

如何解决此问题? 我无法删除"抛出IOException"从每个函数,因为我必须调用" createImageFile()"如果没有"抛出IOException"。

,这是行不通的

一些想法? 谢谢!

1 个答案:

答案 0 :(得分:0)

在您的代码中......

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

是的......你不能在没有try-catch的情况下调用它。方法throws

再次,在你的代码中

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

您将方法称为罚款。你被抓住了#34; throws声明的异常。

如果你想真的"看"错误,不要敬酒。这是一个糟糕的用户界面设计,可以看到随机弹出窗口。

使用日志(并更好地命名dfg

catch (IOException ex) {
    Log.e("ERROR: dispatchTakePictureIntent", ex.getMessage());
}