在我的应用程序中,我想在后台执行位图转换,因为我面临内存泄漏问题。我知道,可以使用Async Task完成,但不知道如何执行此操作。下面我发布了我的代码。
package com.android.album3;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Album3Activity extends Activity
{
File [] mediaFiles;
File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
String name = null;
ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
imageDir = new File(Environment.getExternalStorageDirectory().toString()+
"/diplomat");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
if(imageDir.exists())
{
setContentView(R.layout.grid);
mediaFiles = imageDir.listFiles();
//Log.d("Length of images",""+mediaFiles.length);
for(File file : mediaFiles)
{
bmpArray.add(convertToBitmap(file));
fileName.add(readFileName(file));
Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
Log.d(TAG, "call to convertToBitmap");
}//for
adapter = new ImageAdapter(this, bmpArray, fileName);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
{
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", position);
startActivity(in);
}//onItemClick
});
}//if
else
{
setContentView(R.layout.no_media);
//Toast.makeText(Album3Activity.this, "No files available", Toast.LENGTH_SHORT).show();
}//else
}//onCreate
public static Bitmap convertToBitmap(File file)
{
URL url = null;
try
{
url = file.toURL();
}//try
catch (MalformedURLException e1)
{
Log.d(TAG, e1.toString());
}//catch
Bitmap bmp = null;
try
{
bmp = BitmapFactory.decodeStream(url.openStream());
//bmp.recycle();
}//try
catch(Exception e)
{
Log.d(TAG, "Exception: "+e.toString());
}//catch
return bmp;
}//convertToBitmap
public String readFileName(File file)
{
String name = file.getName();
return name;
}//readFileName
}//class
答案 0 :(得分:0)
你面临着像记忆问题,因为你的图像尺寸很大,你使用像这样的BitmapFactory解码这个图像
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 2;//any integer value greater than 1
Bitmap bm = BitmapFactory.decodeFile(root.getPath() + "/"
+ imageName,option);
imageView.setImageBitmap(bm);
如果设置为值&gt;如图1所示,请求解码器对原始图像进行二次采样,返回较小的图像以节省存储器。样本大小是任一维度中对应于解码位图中的单个像素的像素数。例如,inSampleSize == 4返回的图像是原始宽度/高度的1/4,像素数量的1/16。任何值&lt; = 1都被视为1.注意:解码器将尝试满足此请求,但生成的位图可能具有与请求完全相同的不同维度。此外,2的幂通常更快/更容易让解码器兑现。