diff --git a/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java b/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java index 9bbbc8103fd8d8c34daa7a5996ded8e6cd77ed66..f0f77a963dcf577091f5121732abdc348eb50781 100644 --- a/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java +++ b/ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java @@ -190,25 +190,16 @@ public class BitmapLoadTask { private void processInputUri() throws NullPointerException, IOException { String inputUriScheme = mInputUri.getScheme(); if ("http".equals(inputUriScheme) || "https".equals(inputUriScheme)) { - try { + if(mInputUri != null && mOutputUri != null) { downloadFile(mInputUri, mOutputUri); - } catch (NullPointerException e) { - LogUtils.LogError(TAG, "Downloading failed:"+e); - throw e; } } else if ("content".equals(inputUriScheme)) { - try { + if(mInputUri != null && mOutputUri != null) { copyFile(mInputUri, mOutputUri); - } catch (NullPointerException | IOException e) { - LogUtils.LogError(TAG, "Copying failed:"+e); - throw e; } } else if("dataability".equals(inputUriScheme)){ - try { + if(mInputUri != null && mOutputUri != null) { copyFile(mInputUri, mOutputUri); - } catch (NullPointerException | IOException e) { - LogUtils.LogError(TAG, "Copying failed:"+e); - throw e; } } else if (!"file".equals(inputUriScheme)) { LogUtils.LogError(TAG, "Invalid Uri scheme " + inputUriScheme); @@ -216,12 +207,12 @@ public class BitmapLoadTask { } } - private void copyFile(Uri inputUri, Uri outputUri) throws NullPointerException, IOException { + private void copyFile(Uri inputUri, Uri outputUri) throws IOException { LogUtils.LogDebug(TAG, "copyFile"); System.out.println(inputUri); if (outputUri == null) { - throw new NullPointerException("Output Uri is null - cannot copy image"); + return; } InputStream inputStream; @@ -231,18 +222,24 @@ public class BitmapLoadTask { //inputStream = mContext.getContentResolver().openInputStream(inputUri); //通过InputUri获取InputStream DataAbilityHelper helper = DataAbilityHelper.creator(mContext); - FileDescriptor fd_i = helper.openFile(mInputUri, "r"); - inputStream = new FileInputStream(fd_i); - FileDescriptor fd_o = helper.openFile(outputUri,"w"); - outputStream = new FileOutputStream(fd_o); - if (inputStream == null) { - throw new NullPointerException("InputStream for given input Uri is null"); - } + if(helper != null) { + FileDescriptor fd_i = helper.openFile(mInputUri, "r"); + if (fd_i != null) { + inputStream = new FileInputStream(fd_i); + FileDescriptor fd_o = helper.openFile(outputUri, "w"); + if (fd_o != null) { + outputStream = new FileOutputStream(fd_o); + if (inputStream == null) { + return; + } - byte[] buffer = new byte[1024]; - int length; - while ((length = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, length); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, length); + } + } + } } } catch (DataAbilityRemoteException e) { e.printStackTrace(); @@ -253,11 +250,11 @@ public class BitmapLoadTask { } } - private void downloadFile( Uri inputUri, Uri outputUri) throws NullPointerException, IOException { + private void downloadFile( Uri inputUri, Uri outputUri) throws IOException { LogUtils.LogDebug(TAG, "downloadFile"); if (outputUri == null) { - throw new NullPointerException("Output Uri is null - cannot download image"); + return; } OkHttpClient client = new OkHttpClient(); @@ -278,13 +275,16 @@ public class BitmapLoadTask { //通过OutputUri获取OutputStream OutputStream outputStream; DataAbilityHelper helper = DataAbilityHelper.creator(mContext); - FileDescriptor fd = helper.openFile(outputUri,"w"); - outputStream = new FileOutputStream(fd); - if (outputStream != null) { - sink = Okio.sink(outputStream); - source.readAll(sink); - } else { - throw new NullPointerException("OutputStream for given output Uri is null"); + if(helper != null) { + FileDescriptor fd = helper.openFile(outputUri, "w"); + if (fd != null) { + outputStream = new FileOutputStream(fd); + if (outputStream != null) { + sink = Okio.sink(outputStream); + source.readAll(sink); + } else { + } + } } } catch (DataAbilityRemoteException e) { e.printStackTrace();