Example usage for android.support.v4.provider DocumentFile canWrite

List of usage examples for android.support.v4.provider DocumentFile canWrite

Introduction

In this page you can find the example usage for android.support.v4.provider DocumentFile canWrite.

Prototype

public abstract boolean canWrite();

Source Link

Usage

From source file:com.veniosg.dir.mvvm.model.storage.access.SafStorageAccessManager.java

private boolean checkWriteAccess(File fileInStorage) {
    File fileParent = fileInStorage.getParentFile();
    // Reached root, can't write
    if (fileParent == null)
        return false;
    // Recur until we find a parent that exists
    if (!fileParent.exists())
        return checkWriteAccess(fileParent);

    File tmpFile = generateDummyFileIn(fileParent);

    boolean writable = false;
    if (isWritable(tmpFile))
        writable = true;/*from  ww  w.  ja  v  a2s  .c  o  m*/

    DocumentFile document;
    if (!writable) {
        // Java said not writable, confirm with SAF
        document = createFile(context, tmpFile, "image/png");

        if (document != null) {
            writable = document.canWrite() && tmpFile.exists();
        }
    }

    // Cleanup
    safAwareDelete(context, tmpFile);
    return writable;
}

From source file:com.commonsware.android.tte.DocumentStorageService.java

private void load(Uri document) {
    try {//from  ww w  .j  a v  a 2  s.  com
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());

        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }

        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);

                try {
                    String text = slurp(is);
                    DocumentFile docFile;

                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }

                    EventBus.getDefault().post(
                            new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

@Override
public boolean canWrite(File file) {
    if (file.canWrite()) {
        return true;
    }//from w w w .ja  v a2s. c  om

    DocumentFile f = getDocument(app, file);

    return f != null && f.canWrite();
}

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

@TargetApi(19)
private static DocumentFile getOutputMediaDocumentFile() {
    DocumentFile saveDir = PluginManager.getSaveDirNew(false);

    if (saveDir == null || !saveDir.exists() || !saveDir.canWrite()) {
        saveDir = PluginManager.getSaveDirNew(true);
    }//  w  w  w .  ja v  a 2s  .c  o m

    Calendar d = Calendar.getInstance();
    String fileFormat = String.format("%04d%02d%02d_%02d%02d%02d", d.get(Calendar.YEAR),
            d.get(Calendar.MONTH) + 1, d.get(Calendar.DAY_OF_MONTH), d.get(Calendar.HOUR_OF_DAY),
            d.get(Calendar.MINUTE), d.get(Calendar.SECOND));

    documentFileSaved = saveDir.createFile("video/mp4", fileFormat);
    return documentFileSaved;
}

From source file:com.almalence.opencam.PluginManagerBase.java

private void saveInputFileNew(boolean isYUV, Long SessionID, int i, byte[] buffer, int yuvBuffer,
        String fileFormat) {/*ww  w.jav a 2  s. c  om*/

    int mImageWidth = Integer.parseInt(PluginManager.getInstance().getFromSharedMem("imageWidth" + SessionID));
    int mImageHeight = Integer
            .parseInt(PluginManager.getInstance().getFromSharedMem("imageHeight" + SessionID));
    ContentValues values = null;
    String resultOrientation = getFromSharedMem("frameorientation" + (i + 1) + Long.toString(SessionID));
    if (resultOrientation == null) {
        resultOrientation = getFromSharedMem("frameorientation" + i + Long.toString(SessionID));
    }

    String resultMirrored = getFromSharedMem("framemirrored" + (i + 1) + Long.toString(SessionID));
    if (resultMirrored == null) {
        resultMirrored = getFromSharedMem("framemirrored" + i + Long.toString(SessionID));
    }

    Boolean cameraMirrored = false;
    if (resultMirrored != null)
        cameraMirrored = Boolean.parseBoolean(resultMirrored);

    int mDisplayOrientation = 0;
    if (resultOrientation != null) {
        mDisplayOrientation = Integer.parseInt(resultOrientation);
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ApplicationScreen.getMainContext());
    boolean saveGeoInfo = prefs.getBoolean("useGeoTaggingPrefExport", false);

    DocumentFile file;
    DocumentFile saveDir = getSaveDirNew(false);
    if (saveDir == null || !saveDir.exists()) {
        return;
    }

    file = saveDir.createFile("image/jpeg", fileFormat);
    if (file == null || !file.canWrite()) {
        return;
    }

    OutputStream os = null;
    File bufFile = new File(ApplicationScreen.instance.getFilesDir(), "buffer.jpeg");
    try {
        os = new FileOutputStream(bufFile);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (os != null) {
        try {
            if (!isYUV) {
                os.write(buffer);
            } else {
                jpegQuality = Integer.parseInt(prefs.getString(MainScreen.sJPEGQualityPref, "95"));

                com.almalence.YuvImage image = new com.almalence.YuvImage(yuvBuffer, ImageFormat.NV21,
                        mImageWidth, mImageHeight, null);
                // to avoid problems with SKIA
                int cropHeight = image.getHeight() - image.getHeight() % 16;
                image.compressToJpeg(new Rect(0, 0, image.getWidth(), cropHeight), jpegQuality, os);
            }
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mDisplayOrientation = saveExifToInput(bufFile, mDisplayOrientation, cameraMirrored, saveGeoInfo);

        // Copy buffer image with exif tags into result file.
        InputStream is = null;
        int len;
        byte[] buf = new byte[1024];
        try {
            os = ApplicationScreen.instance.getContentResolver().openOutputStream(file.getUri());
            is = new FileInputStream(bufFile);
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
            is.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    bufFile.delete();

    values = new ContentValues();
    values.put(ImageColumns.TITLE, file.getName().substring(0, file.getName().lastIndexOf(".")));
    values.put(ImageColumns.DISPLAY_NAME, file.getName());
    values.put(ImageColumns.DATE_TAKEN, System.currentTimeMillis());
    values.put(ImageColumns.MIME_TYPE, "image/jpeg");
    values.put(ImageColumns.ORIENTATION, mDisplayOrientation);

    String filePath = file.getName();
    // If we able to get File object, than get path from it.
    // fileObject should not be null for files on phone memory.
    File fileObject = Util.getFileFromDocumentFile(file);
    if (fileObject != null) {
        filePath = fileObject.getAbsolutePath();
        values.put(ImageColumns.DATA, filePath);
    } else {
        // This case should typically happen for files saved to SD
        // card.
        String documentPath = Util.getAbsolutePathFromDocumentFile(file);
        values.put(ImageColumns.DATA, documentPath);
    }

    if (saveGeoInfo) {
        Location l = MLocation.getLocation(ApplicationScreen.getMainContext());
        if (l != null) {
            values.put(ImageColumns.LATITUDE, l.getLatitude());
            values.put(ImageColumns.LONGITUDE, l.getLongitude());
        }
    }

    ApplicationScreen.instance.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}