Java Code Examples for com.nostra13.universalimageloader.utils.IoUtils#closeSilently()

The following examples show how to use com.nostra13.universalimageloader.utils.IoUtils#closeSilently() . 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: LruDiscCache.java    From letv with Apache License 2.0 6 votes vote down vote up
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
    boolean z = false;
    Editor editor = this.cache.edit(getKey(imageUri));
    if (editor != null) {
        OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), this.bufferSize);
        z = false;
        try {
            z = bitmap.compress(this.compressFormat, this.compressQuality, os);
            if (z) {
                editor.commit();
            } else {
                editor.abort();
            }
        } finally {
            IoUtils.closeSilently(os);
        }
    }
    return z;
}
 
Example 2
Source File: BaseImageDecoder.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
/**
 * Decodes image from URI into {@link android.graphics.Bitmap}. Image is scaled close to incoming {@linkplain com.nostra13.universalimageloader.core.assist.ImageSize target size}
 * during decoding (depend on incoming parameters).
 *
 * @param decodingInfo Needed data for decoding image
 * @return Decoded bitmap
 * @throws java.io.IOException                   if some I/O exception occurs during image reading
 * @throws UnsupportedOperationException if image URI has unsupported scheme(protocol)
 */
@Override
public Bitmap decode(ImageDecodingInfo decodingInfo) throws IOException {
	Bitmap decodedBitmap;
	ImageFileInfo imageInfo;

	InputStream imageStream = getImageStream(decodingInfo);
	try {
		imageInfo = defineImageSizeAndRotation(imageStream, decodingInfo);
		imageStream = resetStream(imageStream, decodingInfo);
		Options decodingOptions = prepareDecodingOptions(imageInfo.imageSize, decodingInfo);
		decodedBitmap = BitmapFactory.decodeStream(imageStream, null, decodingOptions);
	} finally {
		IoUtils.closeSilently(imageStream);
	}

	if (decodedBitmap == null) {
		L.e(ERROR_CANT_DECODE_IMAGE, decodingInfo.getImageKey());
	} else {
		decodedBitmap = considerExactScaleAndOrientatiton(decodedBitmap, decodingInfo, imageInfo.exif.rotation,
				imageInfo.exif.flipHorizontal);
	}
	return decodedBitmap;
}
 
Example 3
Source File: BaseDiscCache.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
@Override
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
	File imageFile = getFile(imageUri);
	File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
	OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
	boolean savedSuccessfully = false;
	try {
		savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
	} finally {
		IoUtils.closeSilently(os);
		if (savedSuccessfully && !tmpFile.renameTo(imageFile)) {
			savedSuccessfully = false;
		}
		if (!savedSuccessfully) {
			tmpFile.delete();
		}
	}
	bitmap.recycle();
	return savedSuccessfully;
}
 
Example 4
Source File: LruDiskCache.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 6 votes vote down vote up
/**
 * 把图片bitmap存入到缓存器中
 * @param imageUri Original image URI  图片URL地址
 * @param bitmap   Image bitmap   需要保存的图片
 * @return
 * @throws IOException
 */
@Override
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
	DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
	if (editor == null) {
		return false;
	}
       //流写入文件
	OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
	boolean savedSuccessfully = false;
	try {
		savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
	} finally {
		IoUtils.closeSilently(os);
	}
	if (savedSuccessfully) {
		editor.commit();
	} else {
		editor.abort();
	}
	return savedSuccessfully;
}
 
Example 5
Source File: BaseDiskCache.java    From WliveTV with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
	File imageFile = getFile(imageUri);
	File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
	OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
	boolean savedSuccessfully = false;
	try {
		savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
	} finally {
		IoUtils.closeSilently(os);
		if (savedSuccessfully && !tmpFile.renameTo(imageFile)) {
			savedSuccessfully = false;
		}
		if (!savedSuccessfully) {
			tmpFile.delete();
		}
	}
	bitmap.recycle();
	return savedSuccessfully;
}
 
Example 6
Source File: LruDiscCache.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
	DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
	if (editor == null) {
		return false;
	}

	OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
	boolean copied = false;
	try {
		copied = IoUtils.copyStream(imageStream, os, listener, bufferSize);
	} finally {
		IoUtils.closeSilently(os);
		if (copied) {
			editor.commit();
		} else {
			editor.abort();
		}
	}
	return copied;
}
 
Example 7
Source File: LruDiskCache.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 6 votes vote down vote up
/**
 * 把图片流数据保存到缓存中
 * @param imageUri    Original image URI  图像URL连接地址
 * @param imageStream Input stream of image (shouldn't be closed in this method)  图片流
 * @param listener    图片流拷贝监听器
 * Listener for saving progress, can be ignored if you don't use
 *                    {@linkplain com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener
 *                    progress listener} in ImageLoader calls
 * @return
 * @throws IOException
 */
@Override
public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
	DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
	if (editor == null) {
		return false;
	}
       //流写入文件
	OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
	boolean copied = false;
	try {
		copied = IoUtils.copyStream(imageStream, os, listener, bufferSize);
	} finally {
		IoUtils.closeSilently(os);
		if (copied) {
			editor.commit();
		} else {
			editor.abort();
		}
	}
	return copied;
}
 
Example 8
Source File: BaseDiskCache.java    From candybar with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
    File imageFile = getFile(imageUri);
    File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
    boolean loaded = false;
    try {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
        try {
            loaded = IoUtils.copyStream(imageStream, os, listener, bufferSize);
        } finally {
            IoUtils.closeSilently(os);
        }
    } finally {
        if (loaded && !tmpFile.renameTo(imageFile)) {
            loaded = false;
        }
        if (!loaded) {
            tmpFile.delete();
        }
    }
    return loaded;
}
 
Example 9
Source File: LruDiscCache.java    From letv with Apache License 2.0 6 votes vote down vote up
public boolean save(String imageUri, InputStream imageStream, CopyListener listener) throws IOException {
    boolean z = false;
    Editor editor = this.cache.edit(getKey(imageUri));
    if (editor != null) {
        OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), this.bufferSize);
        z = false;
        try {
            z = IoUtils.copyStream(imageStream, os, listener, this.bufferSize);
        } finally {
            IoUtils.closeSilently(os);
            if (z) {
                editor.commit();
            } else {
                editor.abort();
            }
        }
    }
    return z;
}
 
Example 10
Source File: BaseDiscCache.java    From letv with Apache License 2.0 6 votes vote down vote up
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
    File imageFile = getFile(imageUri);
    File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), this.bufferSize);
    boolean savedSuccessfully = false;
    try {
        savedSuccessfully = bitmap.compress(this.compressFormat, this.compressQuality, os);
        bitmap.recycle();
        return savedSuccessfully;
    } finally {
        IoUtils.closeSilently(os);
        if (savedSuccessfully && !tmpFile.renameTo(imageFile)) {
            savedSuccessfully = false;
        }
        if (!savedSuccessfully) {
            tmpFile.delete();
        }
    }
}
 
Example 11
Source File: LruDiskCache.java    From candybar with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
    DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
    if (editor == null) {
        return false;
    }

    OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
    boolean savedSuccessfully = false;
    try {
        savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
    } finally {
        IoUtils.closeSilently(os);
    }
    if (savedSuccessfully) {
        editor.commit();
    } else {
        editor.abort();
    }
    return savedSuccessfully;
}
 
Example 12
Source File: LruDiskCache.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException {
	DiskLruCache.Editor editor = cache.edit(getKey(imageUri));
	if (editor == null) {
		return false;
	}

	OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize);
	boolean copied = false;
	try {
		copied = IoUtils.copyStream(imageStream, os, listener, bufferSize);
	} finally {
		IoUtils.closeSilently(os);
		if (copied) {
			editor.commit();
		} else {
			editor.abort();
		}
	}
	return copied;
}
 
Example 13
Source File: BaseDiskCache.java    From candybar with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
    File imageFile = getFile(imageUri);
    File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), bufferSize);
    boolean savedSuccessfully = false;
    try {
        savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os);
    } finally {
        IoUtils.closeSilently(os);
        if (savedSuccessfully && !tmpFile.renameTo(imageFile)) {
            savedSuccessfully = false;
        }
        if (!savedSuccessfully) {
            tmpFile.delete();
        }
    }
    bitmap.recycle();
    return savedSuccessfully;
}
 
Example 14
Source File: LoadAndDisplayImageTask.java    From WliveTV with Apache License 2.0 5 votes vote down vote up
private boolean downloadImage() throws IOException {
	InputStream is = getDownloader().getStream(uri, options.getExtraForDownloader());
	if (is == null) {
		L.e(ERROR_NO_IMAGE_STREAM, memoryCacheKey);
		return false;
	} else {
		try {
			return configuration.diskCache.save(uri, is, this);
		} finally {
			IoUtils.closeSilently(is);
		}
	}
}
 
Example 15
Source File: BaseImageDecoder.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
protected InputStream resetStream(InputStream imageStream, ImageDecodingInfo decodingInfo) throws IOException {
    if (imageStream.markSupported()) {
        try {
            imageStream.reset();
            return imageStream;
        } catch (IOException ignored) {
        }
    }
    IoUtils.closeSilently(imageStream);
    return getImageStream(decodingInfo);
}
 
Example 16
Source File: BaseImageDecoder.java    From candybar with Apache License 2.0 5 votes vote down vote up
protected InputStream resetStream(InputStream imageStream, ImageDecodingInfo decodingInfo) throws IOException {
    if (imageStream.markSupported()) {
        try {
            imageStream.reset();
            return imageStream;
        } catch (IOException ignored) {
        }
    }
    IoUtils.closeSilently(imageStream);
    return getImageStream(decodingInfo);
}
 
Example 17
Source File: BaseImageDecoder.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
protected InputStream resetStream(InputStream imageStream, ImageDecodingInfo decodingInfo) throws IOException {
	try {
		imageStream.reset();
	} catch (IOException e) {
		IoUtils.closeSilently(imageStream);
		imageStream = getImageStream(decodingInfo);
	}
	return imageStream;
}
 
Example 18
Source File: LoadAndDisplayImageTask.java    From candybar with Apache License 2.0 5 votes vote down vote up
private boolean downloadImage() throws IOException {
    InputStream is = getDownloader().getStream(uri, options.getExtraForDownloader());
    if (is == null) {
        L.e(ERROR_NO_IMAGE_STREAM, memoryCacheKey);
        return false;
    } else {
        try {
            return configuration.diskCache.save(uri, is, this);
        } finally {
            IoUtils.closeSilently(is);
        }
    }
}
 
Example 19
Source File: LoadAndDisplayImageTask.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
private boolean downloadImage() throws IOException {
	InputStream is = getDownloader().getStream(uri, options.getExtraForDownloader());
	if (is == null) {
		L.e(ERROR_NO_IMAGE_STREAM, memoryCacheKey);
		return false;
	} else {
		try {
			return configuration.diskCache.save(uri, is, this);
		} finally {
			IoUtils.closeSilently(is);
		}
	}
}
 
Example 20
Source File: BaseImageDecoder.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes image from URI into {@link Bitmap}. Image is scaled close to incoming {@linkplain ImageSize target size}
 * during decoding (depend on incoming parameters).
 *
 * @param decodingInfo Needed data for decoding image
 * @return Decoded bitmap
 * @throws IOException                   if some I/O exception occurs during image reading
 * @throws UnsupportedOperationException if image URI has unsupported scheme(protocol)
 */
@Override
public Bitmap decode(ImageDecodingInfo decodingInfo) throws IOException {
    Bitmap decodedBitmap;
    ImageFileInfo imageInfo;

    InputStream imageStream = getImageStream(decodingInfo);
    if (imageStream == null) {
        L.e(ERROR_NO_IMAGE_STREAM, decodingInfo.getImageKey());
        return null;
    }
    try {
        imageInfo = defineImageSizeAndRotation(imageStream, decodingInfo);
        imageStream = resetStream(imageStream, decodingInfo);
        Options decodingOptions = prepareDecodingOptions(imageInfo.imageSize, decodingInfo);
        decodedBitmap = BitmapFactory.decodeStream(imageStream, null, decodingOptions);
    } finally {
        IoUtils.closeSilently(imageStream);
    }

    if (decodedBitmap == null) {
        L.e(ERROR_CANT_DECODE_IMAGE, decodingInfo.getImageKey());
    } else {
        decodedBitmap = considerExactScaleAndOrientatiton(decodedBitmap, decodingInfo, imageInfo.exif.rotation,
                imageInfo.exif.flipHorizontal);
    }
    return decodedBitmap;
}