Search in sources :

Example 71 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class AndroidImplementation method createIntentForURL.

private Intent createIntentForURL(String url) {
    Intent intent;
    Uri uri;
    try {
        if (url.startsWith("intent")) {
            intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        } else {
            if (url.startsWith("/") || url.startsWith("file:")) {
                if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to open the file")) {
                    return null;
                }
            }
            intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            if (url.startsWith("/")) {
                File f = new File(url);
                Uri furi = null;
                try {
                    furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
                } catch (Exception ex) {
                    f = makeTempCacheCopy(f);
                    furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
                }
                if (Build.VERSION.SDK_INT < 21) {
                    List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
                    for (ResolveInfo resolveInfo : resInfoList) {
                        String packageName = resolveInfo.activityInfo.packageName;
                        getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    }
                }
                uri = furi;
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                if (url.startsWith("file:")) {
                    File f = new File(removeFilePrefix(url));
                    System.out.println("File size: " + f.length());
                    Uri furi = null;
                    try {
                        furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
                    } catch (Exception ex) {
                        f = makeTempCacheCopy(f);
                        furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
                    }
                    if (Build.VERSION.SDK_INT < 21) {
                        List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
                        for (ResolveInfo resolveInfo : resInfoList) {
                            String packageName = resolveInfo.activityInfo.packageName;
                            getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        }
                    }
                    uri = furi;
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } else {
                    uri = Uri.parse(url);
                }
            }
            String mimeType = getMimeType(url);
            if (mimeType != null) {
                intent.setDataAndType(uri, mimeType);
            } else {
                intent.setData(uri);
            }
        }
        return intent;
    } catch (Exception err) {
        com.codename1.io.Log.e(err);
        return null;
    }
}
Also used : Uri(android.net.Uri) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JSONException(org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MediaException(com.codename1.media.AsyncMedia.MediaException) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 72 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class AndroidImplementation method openFileOutputStream.

/**
 * @inheritDoc
 */
public OutputStream openFileOutputStream(String file) throws IOException {
    file = removeFilePrefix(file);
    OutputStream os = null;
    try {
        os = createFileOuputStream(file);
    } catch (FileNotFoundException fne) {
        // ask for the permission from the user
        if (fne.getMessage().contains("Permission denied")) {
            if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to access the file")) {
                // The user refused to give access.
                return null;
            } else {
                // The user gave permission try again to access the path
                return createFileOuputStream(file);
            }
        } else {
            throw fne;
        }
    }
    return os;
}
Also used : BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream)

Example 73 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class AndroidImplementation method createMedia.

/**
 * @inheritDoc
 */
@Override
public Media createMedia(final String uri, boolean isVideo, final Runnable onCompletion) throws IOException {
    if (getActivity() == null) {
        return null;
    }
    if (!uri.startsWith(FileSystemStorage.getInstance().getAppHomePath())) {
        if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to play media")) {
            return null;
        }
    }
    if (uri.startsWith("file://")) {
        return createMedia(removeFilePrefix(uri), isVideo, onCompletion);
    }
    File file = null;
    if (uri.indexOf(':') < 0) {
        // use a file object to play to try and workaround this issue:
        // http://code.google.com/p/android/issues/detail?id=4124
        file = new File(uri);
    }
    Media retVal;
    if (isVideo) {
        final AndroidImplementation.Video[] video = new AndroidImplementation.Video[1];
        final boolean[] flag = new boolean[1];
        final File f = file;
        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                VideoView v = new VideoView(getActivity());
                v.setZOrderMediaOverlay(true);
                if (f != null) {
                    v.setVideoURI(Uri.fromFile(f));
                } else {
                    v.setVideoURI(Uri.parse(uri));
                }
                video[0] = new AndroidImplementation.Video(v, getActivity(), onCompletion);
                flag[0] = true;
                synchronized (flag) {
                    flag.notify();
                }
            }
        });
        while (!flag[0]) {
            synchronized (flag) {
                try {
                    flag.wait(100);
                } catch (InterruptedException ex) {
                }
            }
        }
        return video[0];
    } else {
        MediaPlayer player;
        if (file != null) {
            FileInputStream is = new FileInputStream(file);
            player = new MediaPlayer();
            player.setDataSource(is.getFD());
            player.prepare();
        } else {
            player = MediaPlayer.create(getActivity(), Uri.parse(uri));
        }
        retVal = new Audio(getActivity(), player, null, onCompletion);
    }
    return retVal;
}
Also used : AsyncMedia(com.codename1.media.AsyncMedia) AbstractMedia(com.codename1.media.AbstractMedia) Media(com.codename1.media.Media) Audio(com.codename1.media.Audio) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) MediaPlayer(android.media.MediaPlayer)

Example 74 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class AndroidImplementation method writeServiceProperties.

public static void writeServiceProperties(Context a) {
    if (servicePropertiesDirty()) {
        Map<String, String> out = getServiceProperties(a);
        for (String key : servicePropertyKeys) {
            String val = Display.getInstance().getProperty(key, null);
            if (val != null) {
                out.put(key, val);
            }
            if ("true".equals(Display.getInstance().getProperty(key + "#delete", null))) {
                out.remove(key);
            }
        }
        OutputStream os = null;
        try {
            os = a.openFileOutput("CN1$AndroidServiceProperties", 0);
            if (os == null) {
                System.out.println("Failed to save service properties null output stream");
                return;
            }
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeInt(out.size());
            for (String key : out.keySet()) {
                dos.writeUTF(key);
                dos.writeUTF((String) out.get(key));
            }
            serviceProperties = null;
        } catch (FileNotFoundException ex) {
            System.out.println("Service properties file not found.  This is normal for the first run.   On subsequent runs, the file should exist.");
        } catch (IOException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (os != null)
                    os.close();
            } catch (Throwable ex) {
                Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
Also used : BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 75 with File

use of com.codename1.io.File in project CodenameOne by codenameone.

the class AndroidImplementation method copy.

public static void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    try {
        OutputStream out = new FileOutputStream(dst);
        try {
            // Transfer bytes from in to out
            byte[] buf = new byte[8096];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) Paint(android.graphics.Paint)

Aggregations

IOException (java.io.IOException)76 File (java.io.File)61 FileInputStream (java.io.FileInputStream)43 InputStream (java.io.InputStream)33 EncodedImage (com.codename1.ui.EncodedImage)23 FileOutputStream (java.io.FileOutputStream)23 OutputStream (java.io.OutputStream)22 SortedProperties (com.codename1.ant.SortedProperties)18 FileSystemStorage (com.codename1.io.FileSystemStorage)17 BufferedInputStream (com.codename1.io.BufferedInputStream)15 Image (com.codename1.ui.Image)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 RandomAccessFile (java.io.RandomAccessFile)14 ArrayList (java.util.ArrayList)14 EditableResources (com.codename1.ui.util.EditableResources)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 Properties (java.util.Properties)12 File (com.codename1.io.File)11 BufferedImage (java.awt.image.BufferedImage)11