-1

This is my code:

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

}

How can I fix this problem? I can't remove "throws IOException" from each functions, because I have to call "createImageFile()" and this doesn't works without "throws IOException".

Some ideas? Thank you!

Jr Antonio
  • 193
  • 1
  • 4
  • 20

1 Answers1

0

In your code...

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

Right... You can't call it without try-catch-ing. The method throws


Again, in your code

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

You called the method fine. You "caught" the exception that is being declared by throws.

If you want to actually "see" the error, don't Toast. That is a bad user-interface design to see a random pop-up.

Use a Log (and name dfg better)

catch (IOException ex) {
    Log.e("ERROR: dispatchTakePictureIntent", ex.getMessage());
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245