Search in sources :

Example 1 with UniFileInputStreamPipe

use of com.hippo.io.UniFileInputStreamPipe in project EhViewer by seven332.

the class MainActivity method saveImageToTempFile.

private File saveImageToTempFile(UniFile file) {
    if (null == file) {
        return null;
    }
    Bitmap bitmap = null;
    try {
        bitmap = BitmapUtils.decodeStream(new UniFileInputStreamPipe(file), -1, -1, 500 * 500, false, false, null);
    } catch (OutOfMemoryError e) {
    // Ignore
    }
    if (null == bitmap) {
        return null;
    }
    File temp = AppConfig.createTempFile();
    if (null == temp) {
        return null;
    }
    OutputStream os = null;
    try {
        os = new FileOutputStream(temp);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
        return temp;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(os);
    }
}
Also used : Bitmap(android.graphics.Bitmap) UniFileInputStreamPipe(com.hippo.io.UniFileInputStreamPipe) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) UniFile(com.hippo.unifile.UniFile) File(java.io.File)

Example 2 with UniFileInputStreamPipe

use of com.hippo.io.UniFileInputStreamPipe 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

Bitmap (android.graphics.Bitmap)2 UniFileInputStreamPipe (com.hippo.io.UniFileInputStreamPipe)2 UniFile (com.hippo.unifile.UniFile)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Context (android.content.Context)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1