Java Code Examples for com.hippo.unifile.UniFile#createFile()

The following examples show how to use com.hippo.unifile.UniFile#createFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
/**
 * @param extension without dot
 */
@Nullable
private OutputStreamPipe openDownloadOutputStreamPipe(int index, @Nullable String extension) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return null;
    }

    extension = fixExtension('.' + extension);
    UniFile file = dir.createFile(generateImageFilename(index, extension));
    if (file != null) {
        return new UniFileOutputStreamPipe(file);
    } else {
        return null;
    }
}
 
Example 2
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 3
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
/**
 * @param extension without dot
 */
@Nullable
private OutputStreamPipe openDownloadOutputStreamPipe(int index, @Nullable String extension) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return null;
    }

    extension = fixExtension('.' + extension);
    UniFile file = dir.createFile(generateImageFilename(index, extension));
    if (file != null) {
        return new UniFileOutputStreamPipe(file);
    } else {
        return null;
    }
}
 
Example 4
Source File: CommonOperations.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example 5
Source File: DownloadsScene.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private void ensureFile() {
    if (mFile == null) {
        UniFile dir = SpiderDen.getGalleryDownloadDir(mInfo);
        if (dir != null && dir.isDirectory()) {
            mFile = dir.createFile(".thumb");
        }
    }
}
 
Example 6
Source File: HeaderImageView.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public boolean save(InputStream is, long length, String mediaType, ProgressNotify notify) {
    OutputStream os = null;
    try {
        boolean autoSave = Settings.getSaveImageAuto() && mName != null;
        if (autoSave) {
            String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mediaType);
            if (TextUtils.isEmpty(extension)) {
                extension = "jpg";
            }
            String filename = mName + '.' + extension;
            UniFile dir = Settings.getImageSaveLocation();
            if (dir != null) {
                mTempFile = dir.createFile(filename);
            } else {
                mTempFile = UniFile.fromFile(NMBAppConfig.createTempFileWithFilename(filename));
            }
        } else {
            mTempFile = UniFile.fromFile(NMBAppConfig.createTempFile());
        }

        if (mTempFile == null) {
            return false;
        }
        os = mTempFile.openOutputStream();
        IOUtils.copy(is, os);

        // Notify media scanner
        if (autoSave) {
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mTempFile.getUri()));
        }

        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(os);
    }
}
 
Example 7
Source File: DownloadsScene.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private void ensureFile() {
    if (mFile == null) {
        UniFile dir = SpiderDen.getGalleryDownloadDir(mInfo);
        if (dir != null && dir.isDirectory()) {
            mFile = dir.createFile(".thumb");
        }
    }
}
 
Example 8
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private boolean copyFromCacheToDownloadDir(int index) {
    if (sCache == null) {
        return false;
    }
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }
    // Find image file in cache
    String key = EhCacheKeyFactory.getImageKey(mGid, index);
    InputStreamPipe pipe = sCache.getInputStreamPipe(key);
    if (pipe == null) {
        return false;
    }

    OutputStream os = null;
    try {
        // Get extension
        String extension;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        pipe.obtain();
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        if (extension != null) {
            extension = '.' + extension;
        } else {
            return false;
        }
        // Fix extension
        extension = fixExtension(extension);
        // Copy from cache to download dir
        UniFile file = dir.createFile(generateImageFilename(index, extension));
        if (file == null) {
            return false;
        }
        os = file.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(os);
        pipe.close();
        pipe.release();
    }
}
 
Example 9
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
private boolean copyFromCacheToDownloadDir(int index) {
    if (sCache == null) {
        return false;
    }
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }
    // Find image file in cache
    String key = EhCacheKeyFactory.getImageKey(mGid, index);
    InputStreamPipe pipe = sCache.getInputStreamPipe(key);
    if (pipe == null) {
        return false;
    }

    OutputStream os = null;
    try {
        // Get extension
        String extension;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        pipe.obtain();
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        if (extension != null) {
            extension = '.' + extension;
        } else {
            return false;
        }
        // Fix extension
        extension = fixExtension(extension);
        // Copy from cache to download dir
        UniFile file = dir.createFile(generateImageFilename(index, extension));
        if (file == null) {
            return false;
        }
        os = file.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(os);
        pipe.close();
        pipe.release();
    }
}