Search in sources :

Example 21 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class SpiderDen method copyFromCacheToDownloadDir.

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();
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) UniFileInputStreamPipe(com.hippo.io.UniFileInputStreamPipe) InputStreamPipe(com.hippo.streampipe.InputStreamPipe) OutputStream(java.io.OutputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException)

Example 22 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class Settings method getDownloadLocation.

@Nullable
public static UniFile getDownloadLocation() {
    UniFile dir = null;
    try {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(getString(KEY_DOWNLOAD_SAVE_SCHEME, null));
        builder.encodedAuthority(getString(KEY_DOWNLOAD_SAVE_AUTHORITY, null));
        builder.encodedPath(getString(KEY_DOWNLOAD_SAVE_PATH, null));
        builder.encodedQuery(getString(KEY_DOWNLOAD_SAVE_QUERY, null));
        builder.encodedFragment(getString(KEY_DOWNLOAD_SAVE_FRAGMENT, null));
        dir = UniFile.fromUri(sContext, builder.build());
    } catch (Throwable e) {
        ExceptionUtils.throwIfFatal(e);
    // Ignore
    }
    return dir != null ? dir : UniFile.fromFile(AppConfig.getDefaultDownloadDir());
}
Also used : UniFile(com.hippo.unifile.UniFile) FavListUrlBuilder(com.hippo.ehviewer.client.data.FavListUrlBuilder) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable)

Example 23 with UniFile

use of com.hippo.unifile.UniFile in project EhViewer by seven332.

the class ImageSearchLayout method setImageUri.

public void setImageUri(@Nullable Uri imageUri) {
    if (null == imageUri) {
        return;
    }
    Context context = getContext();
    UniFile file = UniFile.fromUri(context, imageUri);
    if (null == file) {
        return;
    }
    try {
        int maxSize = context.getResources().getDimensionPixelOffset(R.dimen.image_search_max_size);
        Bitmap bitmap = BitmapUtils.decodeStream(new UniFileInputStreamPipe(file), maxSize, maxSize);
        if (null == bitmap) {
            return;
        }
        File temp = AppConfig.createTempFile();
        if (null == temp) {
            return;
        }
        // TODO ehentai image search is bad when I'm writing this line.
        // Re-compress image will make image search failed.
        OutputStream os = null;
        try {
            os = new FileOutputStream(temp);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
            mImagePath = temp.getPath();
            mPreview.setImageBitmap(bitmap);
            mPreview.setVisibility(VISIBLE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(os);
        }
    } catch (OutOfMemoryError e) {
        Log.e(TAG, "Out of memory");
    }
}
Also used : Context(android.content.Context) Bitmap(android.graphics.Bitmap) UniFile(com.hippo.unifile.UniFile) UniFileInputStreamPipe(com.hippo.io.UniFileInputStreamPipe) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) UniFile(com.hippo.unifile.UniFile)

Aggregations

UniFile (com.hippo.unifile.UniFile)23 IOException (java.io.IOException)10 File (java.io.File)6 OutputStream (java.io.OutputStream)6 Nullable (androidx.annotation.Nullable)5 Intent (android.content.Intent)4 InputStream (java.io.InputStream)4 Uri (android.net.Uri)3 UniFileInputStreamPipe (com.hippo.io.UniFileInputStreamPipe)3 InputStreamPipe (com.hippo.streampipe.InputStreamPipe)3 SuppressLint (android.annotation.SuppressLint)2 Bitmap (android.graphics.Bitmap)2 BitmapFactory (android.graphics.BitmapFactory)2 ListUrlBuilder (com.hippo.ehviewer.client.data.ListUrlBuilder)2 FileOutputStream (java.io.FileOutputStream)2 Context (android.content.Context)1 PackageInfo (android.content.pm.PackageInfo)1 PackageManager (android.content.pm.PackageManager)1 FavListUrlBuilder (com.hippo.ehviewer.client.data.FavListUrlBuilder)1 GalleryInfo (com.hippo.ehviewer.client.data.GalleryInfo)1