解答1:在应用程序中,我加载服务器的4个base64字符串,并在应用程序中创建缩略图。为它们创建视图并将其添加到滚动布局。到目前为止没有问题。
A1-A1-A1 ++如果我一次又一次重复A1-向布局添加5张图像和5张以上图像...-没问题。
A2:仍在应用程序内-与主要活动相反。进入新的活动-图片选择活动。从图库中加载图片,选择应用程序的一部分。预创建图像。到目前为止没有问题。
A2-A2-A2 ++如果我在此之后一次又一次地重复A2-没问题。
A1-A2-A2-A2 ++如果我做A1并一次又一次重复A2-没问题。
A1-A2-A1-A2如果我以A1开头并执行A2,然后先返回A1,然后再返回A2,则在应创建预览图像时会崩溃。
A1:
int count = 1;
while (count < 5)
{
try {
TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei = mngr.getDeviceId();
String image_count = String.valueOf(count);
String user_image = new GetUserImagesActivity().execute(imei,image_count).get();
byte[] decodedString = Base64.decode(user_image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
/*BitmapDrawable ob = new BitmapDrawable(decodedByte);*/
//ImageView Setup
ImageView imageView1 = new ImageView(this);
imageView1.setTag(count);
imageView1.setOnTouchListener(this);
//setting image resource
imageView1.setImageBitmap(decodedByte);
//setting image position
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.gravity=Gravity.CENTER_HORIZONTAL;
params2.gravity=Gravity.CENTER_VERTICAL;
imageView1.setLayoutParams(params2);
imageView1.setAdjustViewBounds(true);
imageView1.setMaxHeight(240);
imageView1.setMaxWidth(180);
/*imageView.setMinimumHeight(180);
imageView.setMinimumWidth(240);*/
imageView1.setId(110011);
imageView1.setPadding(5, 0, 5, 0);
//adding view to layout
top_container.addView(imageView1);
count++;
//Tried this to solve the problem//
imageView1.setDrawingCacheEnabled(true);
imageView1.buildDrawingCache();
//Causes images to get black//
/*decodedByte.recycle();*/
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Toast.makeText( getApplicationContext(), "problem 1" , Toast.LENGTH_LONG ).show();
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Toast.makeText( getApplicationContext(), "problem 2" , Toast.LENGTH_LONG ).show();
}
}
A2:-服务器上的新图像(在预览时失败)
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
bitmap = BitmapFactory.decodeFile(fileSrc);
// load // preview // image
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 640, false);
// bmpDrawable = new BitmapDrawable(bitmapPreview);
img_logo.setImageBitmap(bitmap);
日志:
11-08 16:44:58.293: D/dalvikvm(22771): GC_BEFORE_OOM freed 44K, 34% free 22390K/33571K, paused 23ms
11-08 16:44:58.293: E/dalvikvm-heap(22771): Out of memory on a 36000016-byte allocation.
11-08 16:44:58.303: W/dalvikvm(22771): threadid=1: thread exiting with uncaught exception (group=0x40a9f210)
11-08 16:44:58.303: E/AndroidRuntime(22771): FATAL EXCEPTION: main
11-08 16:44:58.303: E/AndroidRuntime(22771): java.lang.OutOfMemoryError
11-08 16:44:58.303: E/AndroidRuntime(22771): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-08 16:44:58.303: E/AndroidRuntime(22771): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
11-08 16:44:58.303: E/AndroidRuntime(22771): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:305)
解决方案是:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inSampleSize = 6;
bitmap = BitmapFactory.decodeFile(fileSrc,options);
img_logo.setImageBitmap(bitmap);
以及我添加了bao.close(); 和bitmap.recycle(); 制作base64图像字符串并关闭活动时,将执行以下操作:代码如下所示:
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bao);
byte[] ba = bao.toByteArray();
String text = Base64.encodeToString(ba,0);
try {
bao.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap.recycle();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句