Skip to content

Commit 337d74b

Browse files
authored
fix view local files
Fix open local files with url content://
1 parent 1c0d78c commit 337d74b

1 file changed

Lines changed: 122 additions & 4 deletions

File tree

android/src/main/java/com/philipphecht/RNDocViewerModule.java

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,116 @@ private File downloadFile(String url, String fileName, Boolean cache, String fil
261261

262262

263263

264+
} catch (FileNotFoundException e) {
265+
e.printStackTrace();
266+
callback.invoke(ERROR_FILE_NOT_FOUND);
267+
return null;
268+
} catch (IOException e) {
269+
e.printStackTrace();
270+
callback.invoke(ERROR_UNKNOWN_ERROR);
271+
return null;
272+
}
273+
}
274+
private File copyFile(InputStream in, String fileName, Boolean cache, String fileType, byte[] bytesData, Callback callback) {
275+
276+
try {
277+
Context context = getReactApplicationContext().getBaseContext();
278+
File outputDir = context.getCacheDir();
279+
if (bytesData.length > 0) {
280+
// use cache
281+
File f = cache != null && cache ? new File(outputDir, fileName) : File.createTempFile(FILE_TYPE_PREFIX, "." + fileType,
282+
outputDir);
283+
System.out.println("Bytes will be creating a file");
284+
final FileOutputStream fileOutputStream;
285+
try {
286+
fileOutputStream = new FileOutputStream(f);
287+
} catch (FileNotFoundException e) {
288+
e.printStackTrace();
289+
return null;
290+
}
291+
292+
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
293+
fileOutputStream);
294+
try {
295+
bufferedOutputStream.write(bytesData);
296+
} catch (IOException e) {
297+
e.printStackTrace();
298+
return null;
299+
} finally {
300+
try {
301+
bufferedOutputStream.close();
302+
} catch (IOException e) {
303+
e.printStackTrace();
304+
}
305+
}
306+
return f;
307+
} else {
308+
String extension = MimeTypeMap.getFileExtensionFromUrl(fileName);
309+
System.out.println("Extensions DownloadFile " + extension);
310+
if (extension.equals("") && fileType.equals("")) {
311+
extension = "pdf";
312+
System.out.println("extension (default): " + extension);
313+
}
314+
315+
if (fileType != "" && extension.equals("")) {
316+
extension = fileType;
317+
System.out.println("extension (default): " + extension);
318+
}
319+
320+
// check has extension
321+
if (fileName.indexOf("\\.") == -1) {
322+
fileName = fileName + '.' + extension;
323+
}
324+
// if use cache, check exist
325+
if (cache != null && cache) {
326+
File existFile = new File(outputDir, fileName);
327+
if (existFile.exists()) {
328+
return existFile;
329+
}
330+
}
331+
332+
333+
File f;
334+
try {
335+
// use cache
336+
f = cache != null && cache ? new File(outputDir, fileName)
337+
: File.createTempFile(FILE_TYPE_PREFIX, "." + extension, outputDir);
338+
339+
// make sure the receiving app can read this file
340+
f.setReadable(true, false);
341+
System.out.println(f.getPath());
342+
343+
try {
344+
FileOutputStream outStream = new FileOutputStream(f);
345+
try {
346+
// Transfer bytes from in to out
347+
byte[] buf = new byte[1024];
348+
int len;
349+
while ((len = in.read(buf)) > 0) {
350+
outStream.write(buf, 0, len);
351+
}
352+
} finally {
353+
outStream.close();
354+
}
355+
} finally {
356+
in.close();
357+
}
358+
359+
if (f.exists()) {
360+
System.out.println("File exists");
361+
} else {
362+
System.out.println("File doesn't exist");
363+
}
364+
365+
return f;
366+
} catch (Exception err) {
367+
err.printStackTrace();
368+
}
369+
370+
return null;
371+
}
372+
373+
264374
} catch (FileNotFoundException e) {
265375
e.printStackTrace();
266376
callback.invoke(ERROR_FILE_NOT_FOUND);
@@ -316,12 +426,20 @@ public FileDownloaderAsyncTask(Callback callback,
316426

317427
@Override
318428
protected File doInBackground(Void... arg0) {
319-
if (!url.startsWith("file://")) {
320-
System.out.println("Url to download" +url);
429+
if (url.startsWith("content://")) {
430+
File file = null;
431+
try {
432+
InputStream in = getCurrentActivity().getContentResolver().openInputStream(Uri.parse(url));
433+
file = copyFile(in, fileName, cache, fileType, bytesData, callback);
434+
} catch (FileNotFoundException e) {
435+
System.out.println(e);
436+
}
437+
return file;
438+
} else if (!url.startsWith("file://")) {
439+
System.out.println("Url to download" + url);
321440
return downloadFile(url, fileName, cache, fileType, bytesData, callback);
322441
} else {
323-
File file = new File(url.replace("file://", ""));
324-
return file;
442+
return new File(url.replace("file://", ""));
325443
}
326444
}
327445

0 commit comments

Comments
 (0)