7 package com.google.appinventor.components.runtime.util;
9 import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
11 import android.Manifest;
12 import android.os.Environment;
13 import android.util.Log;
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.ByteArrayInputStream;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
38 private static final String LOG_TAG =
FileUtil.class.getSimpleName();
43 private static final String DOCUMENT_DIRECTORY =
"My Documents/";
45 private static final String DIRECTORY_RECORDINGS =
"Recordings";
47 private static final String FILENAME_PREFIX =
"app_inventor_";
54 private static final String DIRECTORY_PICTURES =
"Pictures";
56 private static final String DIRECTORY_DOWNLOADS =
"Downloads";
65 File file =
new File(localFileName);
66 return file.toURI().toString();
79 public static byte[]
readFile(String inputFileName)
throws IOException {
80 Log.w(LOG_TAG,
"Calling deprecated function readFile",
new IllegalAccessException());
91 public static byte[]
readFile(
Form form, String inputFileName)
throws IOException {
92 File inputFile =
new File(inputFileName);
96 if (!inputFile.isFile()) {
97 throw new FileNotFoundException(
"Cannot find file: " + inputFileName);
99 FileInputStream inputStream =
null;
102 inputStream =
openFile(form, inputFileName);
103 int fileLength = (int) inputFile.length();
104 content =
new byte[fileLength];
108 bytesRead = inputStream.read(content, offset, fileLength - offset);
112 if (offset == fileLength) {
115 }
while (bytesRead >= 0);
117 if (inputStream !=
null) {
145 Log.w(LOG_TAG,
"Calling deprecated function openFile",
new IllegalAccessException());
165 public static FileInputStream
openFile(
Form form, String fileName)
throws IOException,
168 form.assertPermission(READ_EXTERNAL_STORAGE);
170 return new FileInputStream(fileName);
194 Log.w(LOG_TAG,
"Calling deprecated function openFile",
new IllegalAccessException());
214 public static FileInputStream
openFile(
Form form, File file)
throws IOException,
216 return openFile(form, file.getAbsolutePath());
240 Log.w(LOG_TAG,
"Calling deprecated function openFile",
new IllegalAccessException());
260 public static FileInputStream
openFile(
Form form, URI fileUri)
throws IOException,
263 form.assertPermission(READ_EXTERNAL_STORAGE);
265 return new FileInputStream(
new File(fileUri));
276 InputStream in =
new URL(url).openStream();
291 public static String
writeFile(
byte[] array, String outputFileName)
throws IOException {
292 InputStream in =
new ByteArrayInputStream(array);
307 public static String
copyFile(String inputFileName, String outputFileName)
309 InputStream in =
new FileInputStream(inputFileName);
324 public static String
writeStreamToFile(InputStream in, String outputFileName)
throws IOException {
325 File file =
new File(outputFileName);
328 file.getParentFile().mkdirs();
330 OutputStream out =
new FileOutputStream(file);
335 return file.toURI().toString();
342 private static void copy(InputStream in, OutputStream out)
throws IOException {
343 out =
new BufferedOutputStream(out, 0x1000);
344 in =
new BufferedInputStream(in, 0x1000);
376 throws IOException, FileException {
377 Log.w(LOG_TAG,
"Calling deprecated function getPictureFile",
new IllegalAccessException());
395 throws IOException, FileException {
396 return getFile(form, DIRECTORY_PICTURES, extension);
418 throws IOException, FileException {
436 throws IOException, FileException {
437 return getFile(form, DIRECTORY_RECORDINGS, extension);
459 throws IOException, FileException {
460 Log.w(LOG_TAG,
"Calling deprecated function getDownloadFile",
new IllegalAccessException());
478 throws IOException, FileException {
479 return getFile(form, DIRECTORY_DOWNLOADS, extension);
493 private static File getFile(
Form form, String category, String extension)
494 throws IOException, FileException {
495 String fileName = DOCUMENT_DIRECTORY + category +
"/" + FILENAME_PREFIX
496 + System.currentTimeMillis() +
"." + extension;
519 public static File
getExternalFile(String fileName)
throws IOException, FileException,
539 throws IOException, FileException, SecurityException {
542 File directory = file.getParentFile();
544 form.assertPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
546 if (!directory.exists() && !directory.mkdirs()) {
547 throw new IOException(
"Unable to create directory " + directory.getAbsolutePath());
550 if (!file.delete()) {
551 throw new IOException(
"Cannot overwrite existing file " + file.getAbsolutePath());
562 String state = Environment.getExternalStorageState();
563 if (Environment.MEDIA_MOUNTED.equals(state)) {
566 if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
578 public static class FileException
extends RuntimeError {
579 private final int msgNumber;
580 public FileException(
int errorMsgNumber) {
581 msgNumber = errorMsgNumber;
584 public int getErrorMessageNumber() {