在Android中,你必须知道é有限的内存,如此大的图像不会适合内存,你会有OutOfMemory异常。
的关键是,在内部存储保存TE图像后,加载它在显示分辨率:
首先下载TE形象,这应该是在UI线程之外完成,让_url
的URL
intance与图像ADDRES和_file
含有字符串目标文件:
URLConnection conn = _url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
boolean success = false; //track succesful operation
if(_file != null)
{
try
{
FileOutputStream fos = new FileOutputStream(_file);
byte data[] = new byte[4086]; //use 4086 bytes buffer
int count = 0;
while ((count = is.read(data)) != -1)
{
fos.write(data, 0, count);//write de data
}
is.close();
fos.flush();
fos.close();
int len = conn.getContentLength();
File f = new File(_file);//check fie length is correct
if(len== f.length())
{
success = true;
}
else
{
//error downloading, delete de file
File tmp = new File(_file);
if(tmp.exists())
{
tmp.delete();
}
}
}catch (Exception e)
{
try
{
e.printStackTrace();
//delete file with errors
File tmp = new File(_file);
if(tmp.exists())
{
tmp.delete();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
finally
{
is.close();//cleanup
}
然后当你有所需的分辨率来加载图像,这里的关键是使用BitmapFactory读取位图信息,并得到缩放位图:
public static Bitmap bitmapFromFile(int width, int height, String file)
{
Bitmap bitmap = null;
final BitmapFactory.Options options = new BitmapFactory.Options();
if(height >0 && width > 0) {
options.inJustDecodeBounds = true;//only read bitmap metadata
BitmapFactory.decodeFile(file,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
}
try
{
bitmap = BitmapFactory.decodeFile(file, options);//decode scaled bitmap
}catch (Throwable t)
{
if(bitmap != null)
{
bitmap.recycle();//cleanup memory, very important!
}
return null;
}
return bitmap
}
的最后一步是计算比例因子:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height;
final int halfWidth = width;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((couldShrink(halfWidth, reqWidth, inSampleSize)&&
couldShrink(halfHeight,reqHeight, inSampleSize))
//&&(halfHeight*halfWidth)/inSampleSize > maxsize)
)
{
inSampleSize *= 2;
}
}
return inSampleSize;
}
private static boolean couldShrink (int dimension, int req_dimension, int divider)
{
int actual = dimension/divider;
int next = dimension/(divider*2);
int next_error = Math.abs(next - req_dimension);
int actual_error = Math.abs(actual-req_dimension);
return next > req_dimension ||
(actual > req_dimension && (next_error < actual_error))
;
}
,如果你想通过做手工也就是说,我建议你使用Picasso将处理donwloading,磁盘缓存和内存缓存你的形象:
要加载到ImageView的名为image
呈现出化背景(R.drawable.img_bg
),而下载:
Picasso.with(image.getContext())
.load(url).placeholder(R.drawable.img_bg).fit()
.into(image, new Callback.EmptyCallback()
{
@Override
public void onSuccess()
{
holder.progress.setVisibility(View.GONE); //hide progress bar
}
@Override
public void onError()
{
holder.progress.setVisibility(View.GONE); //hide progress bar
//do whatever you design to show error
}
});
自己处理位图:
//first declare a target
_target = new Target()
{
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
{
//handle your bitmap (store it and use it on you canvas
}
@Override
public void onBitmapFailed(Drawable errorDrawable)
{
//handle your fail state
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable)
{//for example for drawing a placeholder while downloading
}
};
现在你只需要加载和调整图片大小:
Picasso.with(context).load(url).resize(192, 192).centerCrop().into(_target);
希望有所帮助。
我不能使用这种方法,因为我想保存到内部存储器中,之后我也绘制到画布上 - 我没有图像视图。 – serenskye