2017-02-22 140 views
-1

這是我的代碼的方法:調用有拋出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://stackoverflow.com/a/42330491/2308683 –

+0

「我該如何解決這個問題?」什麼問題?具體的錯誤是什麼? – Alex

回答

0

在你的代碼...

//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更好)

​​