android 扫描本地图片,Android:扫描目录并显示图片(缩略图)(图片未存储在媒体库中)...

前段时间我做的完全相同。您必须将图像所在的文件夹名称传递给setBaseFolder。此方法依次调用refresh(),使用FilenameFilter(代码未包含但很容易实现)从该文件夹中获取名为orig....jpg的所有图像,并将其保存在mFileList中。然后我们调用notifyDataSetChanged(),它将为每个单元格触发getView()。

现在,在getView()中我们要么从缓存中获取缩略图位图,如果我们已经有了它,否则我们制作一个灰色占位符并启动ThumbnailBuilder以创建缩略图。从中获取位图。

我认为你必须稍微更改ThumbnailBuilder,因为我创建了相当大的“缩略图”(500x500),因为我还需要调整大小的图像用于其他目的。此外,当我处理相机拍摄的照片时,有一些东西,根据exif信息旋转图像。但基本上,ThumbnailBuilder只检查是否已有缩略图(我的缩略图图像放在同一个文件夹中,但前缀为small而不是orig_) - 如果缩略图已经存在,我们将其作为Bitmap获取并完成,否则生成图像。最后,在onPostExecute()中,位图设置为ImageView。

public class PhotoAdapter extends BaseAdapter {

private Context mContext;

private int mCellSize;

private File mFolder;

private File[] mFileList;

private Map mThumbnails = new HashMap();

private Set mCreatingTriggered = new HashSet(); // flag that creating already triggered

public PhotoAdapter(Context context, int cellSize) {

mContext = context;

mCellSize = cellSize;

}

@Override

public int getCount() {

if (mFolder == null) {

return 0; // don't do this

} else {

return mFileList.length;

}

}

@Override

public Object getItem(int position) {

return mFileList[position];

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ImageView view = (ImageView)convertView;

if (view == null) {

view = new ImageView(mContext);

view.setLayoutParams(new GridView.LayoutParams(mCellSize, mCellSize));

view.setScaleType(ImageView.ScaleType.CENTER_CROP);

view.setPadding(8, 8, 8, 8);

view.setBackgroundColor(0xFFC6CCD3);

}

Object item = getItem(position);

Bitmap bm = mThumbnails.get(item);

if (bm == null) {

view.setImageBitmap(null);

if (!mCreatingTriggered.contains(item)) {

mCreatingTriggered.add(item);

new ThumbnailBuilder(view, (File)item).execute();

}

} else {

view.setImageBitmap(bm);

}

return view;

}

public void setBaseFolder(File baseFolder) {

if (baseFolder == null) return;

if (!baseFolder.equals(mFolder)) {

releaseThumbnails();

mFolder = baseFolder;

}

refresh();

}

public void refresh() {

if (mFolder == null) {

return;

}

mFileList = mFolder.listFiles(EtbApplication.origImageFilenameFilter);

if (mFileList == null) mFileList = new File[0];

notifyDataSetChanged();

}

public void releaseThumbnails() {

for (Bitmap bm : mThumbnails.values()) {

bm.recycle();

}

mThumbnails.clear();

}

// ------------------------------------------------------------------------------------ Asynchronous Thumbnail builder

private class ThumbnailBuilder extends AsyncTask {

private ImageView mView;

private File mFile;

public ThumbnailBuilder(ImageView view, File file) {

mView = view;

mFile = file;

}

@Override

protected Bitmap doInBackground(Void... params) {

Log.d("adapter", "make small image and thumbnail");

try {

return createThumbnail(mFile.getAbsolutePath());

} catch (Exception e) {

return null;

}

}

@Override

protected void onPostExecute(Bitmap result) {

if (result != null) {

mView.setImageBitmap(result);

mThumbnails.put(mFile, result);

} else {

mView.setImageResource(R.drawable.ic_launcher);

}

}

/**

* Creates Thumbnail (also rotates according to exif-info)

* @param file

* @return

* @throws IOException

*/

private Bitmap createThumbnail(String file) throws IOException {

File thumbnailFile = new File(file.replace("orig_", "small_"));

// If a small image version already exists, just load it and be done.

if (thumbnailFile.exists()) {

return BitmapFactory.decodeFile(thumbnailFile.getAbsolutePath());

}

// Decode image size

BitmapFactory.Options bounds = new BitmapFactory.Options();

bounds.inJustDecodeBounds = true;

BitmapFactory.decodeFile(file, bounds);

if ((bounds.outWidth == -1) || (bounds.outHeight == -1))

return null;

int w, h;

if (bounds.outWidth > bounds.outHeight) { // Querformat

w = 500;

h = 500 * bounds.outHeight / bounds.outWidth;

} else { // Hochformat

h = 500;

w = 500 * bounds.outWidth / bounds.outHeight;

}

BitmapFactory.Options opts = new BitmapFactory.Options();

opts.inSampleSize = 4; // resample -- kleiner aber noch nicht die 500 Pixel, die kommen dann unten

Bitmap resizedBitmap = BitmapFactory.decodeFile(file, opts);

resizedBitmap = Bitmap.createScaledBitmap(resizedBitmap, w, h, true);

ExifInterface exif = new ExifInterface(file);

String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);

int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;

if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;

if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;

if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();

matrix.setRotate(rotationAngle, (float) resizedBitmap.getWidth() / 2, (float) resizedBitmap.getHeight() / 2);

Bitmap rotatedBitmap = Bitmap.createBitmap(resizedBitmap, 0, 0, w, h, matrix, true);

resizedBitmap.recycle();

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

thumbnailFile.createNewFile();

FileOutputStream fo = new FileOutputStream(thumbnailFile);

fo.write(bytes.toByteArray());

fo.close();

//new File(file).delete(); // Originalbild löschen

return rotatedBitmap;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值