尽管我在drawable文件夹中有一个非常小的图像,我从用户那里得到了这个错误.我没有在代码中使用任何位图功能.至少故意:)
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.Resources.getDrawable(Resources.java:1909)
at android.view.View.setBackgroundResource(View.java:16251)
at com.autkusoytas.bilbakalim.SoruEkrani.cevapSecimi(SoruEkrani.java:666)
at com.autkusoytas.bilbakalim.SoruEkrani$9$1.run(SoruEkrani.java:862)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
根据这个stackTrace,我在这一行得到了这个错误('tv'是一个textView):
tv.setBackgroundResource(R.drawable.yanlis);
Run Code Online (Sandbox Code Playgroud)
问题是什么?如果您需要有关代码的其他信息,我可以添加它.谢谢!
我无法理解Image班级和Bitmap班级之间的差异.现在,我知道Bitmap继承自我Image所理解的两者非常相似.有人可以对此有所了解吗?
我想Bitmap在字符串中编码和解码对象base64.我使用的是Android API10,
我试过使用这种形式的方法来编码a,但没有成功Bitmap.
public static String encodeTobase64(Bitmap image) {
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("LOOK", imageEncoded);
return imageEncoded;
}
Run Code Online (Sandbox Code Playgroud) 如何确定/计算位图的字节大小(使用BitmapFactory解码后)?我需要知道它占用了多少内存空间,因为我正在我的应用程序中进行内存缓存/管理.(文件大小不够,因为这些是jpg/png文件)
谢谢你的解决方案!
更新:getRowBytes*getHeight可能会成功.我将以这种方式实现它,直到有人提出反对它的东西.
我用SD解码SD卡的位图BitmapFactory.decodeFile.有时位图大于应用程序需要或堆允许的位图,因此我BitmapFactory.Options.inSampleSize用来请求子采样(较小)位图.
问题是该平台没有强制执行inSampleSize的确切值,有时我的位图太小,或者对于可用内存而言仍然太大.
来自http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:
注意:解码器将尝试满足此请求,但生成的位图可能具有与请求完全相同的不同维度.此外,2的幂通常更快/更容易让解码器兑现.
我应该如何解码SD卡中的位图以获得我需要的确切大小的位图,同时尽可能少地使用内存进行解码呢?
编辑:
当前源代码:
BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
int newWidth = calculateNewWidth(int width, int height);
float sampleSizeF = (float) width / (float) newWidth;
int sampleSize = Math.round(sampleSizeF);
BitmapFactory.Options resample = new BitmapFactory.Options();
resample.inSampleSize = sampleSize; …Run Code Online (Sandbox Code Playgroud) 我正在从Url下载图像并显示它们.在下载时它正在给予out of memory error : bitmap size exceeds VM budget.我正在使用drawable.代码如下:
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity= response.getEntity();
BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap bm = BitmapFactory.decodeStream(instream);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
System.gc();
Run Code Online (Sandbox Code Playgroud)
这是错误: 05-28 14:55:47.251: ERROR/AndroidRuntime(4188): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
我想要一个带有粗体文本的位图图标在地图上绘制它.我有一个片段在图像上写文字:
Bitmap icon = BitmapFactory.decodeResource(PropertyMapList.this.getResources(),
R.drawable.location_mark);
TextPaint paint = new TextPaint();
paint.setColor(Color.BLACK);
paint.setTextSize(14);
paint.setFakeBoldText(true);
//paint.setTextAlign(Align.CENTER);
Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(copy);
//canvas.drawText(jsonObj.getString("district_name"), 5, canvas.getHeight()/2, paint);
String districtName = jsonObj.getString("district_name");
StaticLayout layout = new StaticLayout((districtName.length()>25 ? districtName.substring(0, 24)+"..":districtName)+"\n"+jsonObj.getString("total_properties"), paint, canvas.getWidth()-10,Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false);
canvas.translate(5, canvas.getHeight()/2); //position the text
layout.draw(canvas);
Run Code Online (Sandbox Code Playgroud)
setFakeBoldText(true)不适合我.我希望Bitmap上绘制的文本加粗.
关于使用这些方法时实际发生的情况,文档非常模糊.有人可以解释Matrix实际上如何影响它所设置的位图吗?他们在那里使用了连接这个术语,但是我不清楚这个术语如何应用于坐标数据(之前只用过字符串操作).
我实际上有两个问题:
对于长长的清单感到抱歉,但为了学习,我想探索两种方法......
我有一个URI图像文件,我想减小它的大小上传它.初始图像文件大小取决于从移动设备到移动设备(可以是2MB,可以是500KB),但我希望最终大小约为200KB,以便我可以上传它.
从我读到的,我有(至少)2个选择:
什么是最好的选择?
我想最初调整图像宽度/高度,直到宽度或高度高于1000px(类似1024x768或其他),然后压缩图像质量下降,直到文件大小超过200KB.这是一个例子:
int MAX_IMAGE_SIZE = 200 * 1024; // max final file size
Bitmap bmpPic = BitmapFactory.decodeFile(fileUri.getPath());
if ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
bmpOptions.inSampleSize = 1;
while ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
bmpOptions.inSampleSize++;
bmpPic = BitmapFactory.decodeFile(fileUri.getPath(), bmpOptions);
}
Log.d(TAG, "Resize: " + bmpOptions.inSampleSize);
}
int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
int streamLength = …Run Code Online (Sandbox Code Playgroud)