Java 可从android中的URL有限内存中提取
我想从URL获取图像并将其转换为可绘制的。我有这个方法,效果很好:Java 可从android中的URL有限内存中提取,java,android,memory,Java,Android,Memory,我想从URL获取图像并将其转换为可绘制的。我有这个方法,效果很好: public static Drawable getDrawableFromUrl(String url) throws IOException { Bitmap x; HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.connect(); InputStrea
public static Drawable getDrawableFromUrl(String url) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x);
}
除了一件事。当图像太大时,应用程序崩溃
E/dalvikvm-heap(12750): 13142360-byte external allocation too large for this process.
E/dalvikvm(12750): Out of memory: Heap Size=6023KB, Allocated=3177KB, Bitmap Size=2563KB, Limit=20480KB
E/dalvikvm(12750): Trim info: Footprint=6023KB, Allowed Footprint=6023KB, Trimmed=952KB
E/GraphicsJNI(12750): VM won't let us allocate 13142360 bytes
我是否可以增加图像所需的内存?或者有办法解决这个问题?我建议您将文件临时保存在手机上,然后阅读并将其用作绘图工具。但是,如果手机无法加载图像,则您需要从发件人处缩小图像的比例,或者在打开图像之前缩小图像的比例。我建议您将该文件临时保存在手机上,然后将其读取并用作绘图文件。但是,如果手机无法加载图像,则您需要缩小发件人的图像或在打开图像之前缩小图像。 上面的链接从url返回重新调整大小的位图,然后您可以转换为位图可绘制 从该链接下载并使用位图趣味示例应用程序。 上面的链接从url返回重新调整大小的位图,然后您可以转换为位图可绘制
从该链接下载并使用位图趣味示例应用程序。您需要按照此链接的建议缩小图像的比例或调整其大小 以下是一些用于此的代码片段:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
当你设定你的可画性时,只需调用这个部分
MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mydrawable, 420, 620));
您需要按此链接的建议缩小图像或调整其大小 以下是一些用于此的代码片段:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
当你设定你的可画性时,只需调用这个部分
MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mydrawable, 420, 620));
嗯。下面是最终有效的代码。 正如我建议的那样,我使用给定的函数来缩小图像的大小:calculateInSampleSize-thx Androyds
public static Drawable getDrawableFromUrl(String url, Context context) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bufferedInputStream.mark(connection.getContentLength());
BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.reset();
options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, 400, 400);
x = BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.close();
connection.disconnect();
BitmapDrawable y = new BitmapDrawable(x);
return y;
}
嗯。下面是最终有效的代码。 正如我建议的那样,我使用给定的函数来缩小图像的大小:calculateInSampleSize-thx Androyds
public static Drawable getDrawableFromUrl(String url, Context context) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bufferedInputStream.mark(connection.getContentLength());
BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.reset();
options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, 400, 400);
x = BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.close();
connection.disconnect();
BitmapDrawable y = new BitmapDrawable(x);
return y;
}
对不起,到目前为止,我是一个java noob。我如何称呼它:MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(),mydrawable,420620));在我的getDrawableFromUrl(字符串url)函数中?@baron_bartek您是否试图从url获取图像?是的。我终于有了基于函数的代码。请看我的衣服。对不起,到目前为止我是一个java noob。我如何称呼它:MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(),mydrawable,420620));在我的getDrawableFromUrl(字符串url)函数中?@baron_bartek您是否试图从url获取图像?是的。我终于有了基于函数的代码。请看我的衣服。