java安卓显示目录中的图像
我试图在gallery类型的活动中显示安卓设备上指定目录中的图像。我一直在遵循(至少尝试着)关于在安卓开发者网站上显示图像的指南,但一直迷路。我目前有一个图像的下采样版本,正在将其加载到布局中的图像视图中。xml文件。但这需要我知道我不会出现的图像数量,我担心这是浪费
我需要知道如何根据目录中的图像数量创建图像视图。并在创建的图像视图中显示缩略图或下采样图像
布局。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView 安卓:text="Hello World!" 安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/textView" />
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/mImageView0"
安卓:layout_centerHorizontal="true"
安卓:layout_alignParentEnd="true" />
............
<ImageView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/mImageView5"
安卓:layout_below="@id/mImageView4"
安卓:layout_centerHorizontal="true"
安卓:layout_alignParentEnd="true" />
</RelativeLayout>
主要活动。java
public class MainActivity extends Activity {
private final String TAG = "MainActivity Class";
public ImageView mImageView;
public ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String fileDir = Environment.getExternalStorageDirectory().toString()+ File.separator +"Camera-App-Images"+ File.separator;
File f = new File(fileDir);
Log.d(TAG, fileDir);
File file[] = f.listFiles();
ImageView[] imgViews = new ImageView[file.length];
for (int i=0; i < file.length; i++) {
mImageView = (ImageView)findViewById(R.id.mImageView0 + i);
listView = (ListView)findViewById(R.id.listView);
loadBitmap(fileDir + file[i].getName(), mImageView);
}
}
public void loadBitmap(String fileLoc, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(fileLoc ,imageView);
task.execute();
}
}
位图工作任务
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
private String imagePath = "";
private final String TAG = "BitmapWorkerTask";
public BitmapWorkerTask(String imgPath, ImageView imageView) {
imagePath = imgPath;
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
Log.d(TAG,"Constructor");
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
//data = params[0];
return decodeSampledBitmapFromFile(imagePath, 100, 100);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
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 / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
共 (0) 个答案