com.nostra13.universalimageloader.utils.IoUtils Java Examples

The following examples show how to use com.nostra13.universalimageloader.utils.IoUtils. 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: LruDiskCache.java    From WliveTV 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 #2
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 #3
Source File: LruDiskCache.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 {
    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 #4
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 #5
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 #6
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 #7
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 #8
Source File: BaseImageDownloader.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
/**
 * Retrieves {@link java.io.InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link java.io.InputStream} of image
 * @throws java.io.IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	HttpURLConnection conn = createConnection(imageUri, extra);

	int redirectCount = 0;
	while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
		conn = createConnection(conn.getHeaderField("Location"), extra);
		redirectCount++;
	}

	InputStream imageStream;
	try {
		imageStream = conn.getInputStream();
	} catch (IOException e) {
		// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
		IoUtils.readAndCloseStream(conn.getErrorStream());
		throw e;
	}
	return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}
 
Example #9
Source File: BaseDiskCache.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 {
	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 #10
Source File: BaseDiskCache.java    From BigApp_WordPress_Android 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 #11
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 #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, 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 #13
Source File: CommonUtils.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
public static byte[] getImageByteByBitmap(Bitmap upbitmap) {
    try {
        if (upbitmap == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        upbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int option = 100;
        while (baos.toByteArray().length / 1024 > 120) {
            baos.reset();
            option -= 10;
            upbitmap.compress(Bitmap.CompressFormat.JPEG, option, baos);
        }
        byte[] result = baos.toByteArray();
        IoUtils.closeSilently(baos);
        upbitmap.recycle();
        return result;
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #14
Source File: BaseDiskCache.java    From WliveTV 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 #15
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 #16
Source File: BaseImageDownloader.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves {@link InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link InputStream} of image
 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	HttpURLConnection conn = createConnection(imageUri, extra);

	int redirectCount = 0;
	while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
		conn = createConnection(conn.getHeaderField("Location"), extra);
		redirectCount++;
	}

	InputStream imageStream;
	try {
		imageStream = conn.getInputStream();
	} catch (IOException e) {
		// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
		IoUtils.readAndCloseStream(conn.getErrorStream());
		throw e;
	}
	if (!shouldBeProcessed(conn)) {
		IoUtils.closeSilently(imageStream);
		throw new IOException("Image request failed with response code " + conn.getResponseCode());
	}

	return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}
 
Example #17
Source File: LruDiskCache.java    From WliveTV 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 #18
Source File: BaseImageDownloader.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
protected InputStream getStreamFromNetwork(String s, Object obj)
{
    HttpURLConnection httpurlconnection = createConnection(s, obj);
    for (int i = 0; httpurlconnection.getResponseCode() / 100 == 3 && i < 5; i++)
    {
        httpurlconnection = createConnection(httpurlconnection.getHeaderField("Location"), obj);
    }

    InputStream inputstream;
    try
    {
        inputstream = httpurlconnection.getInputStream();
    }
    catch (IOException ioexception)
    {
        IoUtils.readAndCloseStream(httpurlconnection.getErrorStream());
        throw ioexception;
    }
    return new ContentLengthInputStream(new BufferedInputStream(inputstream, 32768), httpurlconnection.getContentLength());
}
 
Example #19
Source File: BaseDiskCache.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 {
	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 #20
Source File: BaseDiskCache.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 6 votes vote down vote up
/**
 * 进行保存图片(bitmap)到缓存中 其中根据imageurl来生成缓存文件命名
 * @param imageUri Original image URI
 * @param bitmap   Image bitmap
 * @return
 * @throws IOException
 */
@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 #21
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 #22
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 #23
Source File: BaseImageDownloader.java    From Android-Universal-Image-Loader-Modify with Apache License 2.0 6 votes vote down vote up
/**
    * 通过图片的网络地址来获取图片流--当前图片通过网络获取
 * Retrieves {@link InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link InputStream} of image
 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	//创建网络连接
       HttpURLConnection conn = createConnection(imageUri, extra);
       //对于重定向进行判断判断,重定向的次数最大5次循环获取
	int redirectCount = 0;
	while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
		conn = createConnection(conn.getHeaderField("Location"), extra);
		redirectCount++;
	}
       //获取到图像流
	InputStream imageStream;
	try {
		imageStream = conn.getInputStream();
	} catch (IOException e) {
		// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
		IoUtils.readAndCloseStream(conn.getErrorStream());
		throw e;
	}
	if (!shouldBeProcessed(conn)) {
		IoUtils.closeSilently(imageStream);
		throw new IOException("Image request failed with response code " + conn.getResponseCode());
	}
       //对图像流数据进行包装
	return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}
 
Example #24
Source File: MyImageDownloader.java    From repay-android with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves {@link java.io.InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link com.nostra13.universalimageloader.core.DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link java.io.InputStream} of image
 * @throws java.io.IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	HttpURLConnection conn = createConnection(imageUri, extra);

	int redirectCount = 0;
	while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
		conn = createConnection(conn.getHeaderField("Location"), extra);
		redirectCount++;
	}

	InputStream imageStream;
	try {
		imageStream = conn.getInputStream();
	} catch (IOException e) {
		// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
		IoUtils.readAndCloseStream(conn.getErrorStream());
		throw e;
	}
	return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}
 
Example #25
Source File: BaseDiscCache.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 {
	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 {
		IoUtils.closeSilently(imageStream);
		if (loaded && !tmpFile.renameTo(imageFile)) {
			loaded = false;
		}
		if (!loaded) {
			tmpFile.delete();
		}
	}
	return loaded;
}
 
Example #26
Source File: BaseDiscCache.java    From android-open-project-demo 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 #27
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 #28
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, 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 #29
Source File: BaseImageDecoder.java    From android-open-project-demo with Apache License 2.0 6 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);
	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 #30
Source File: BaseImageDownloader.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves {@link InputStream} of image by URI (image is located in the network).
 *
 * @param imageUri Image URI
 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
 * @return {@link InputStream} of image
 * @throws IOException if some I/O error occurs during network request or if no InputStream could be created for
 *                     URL.
 */
protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
	HttpURLConnection conn = createConnection(imageUri, extra);

	int redirectCount = 0;
	while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
		conn = createConnection(conn.getHeaderField("Location"), extra);
		redirectCount++;
	}

	InputStream imageStream;
	try {
		imageStream = conn.getInputStream();
	} catch (IOException e) {
		// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
		IoUtils.readAndCloseStream(conn.getErrorStream());
		throw e;
	}
	return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
}