【问题标题】:Android: Scanning a directory and displaying pictures (thumbnails) (pictures are not stored in the mediastore)Android:扫描目录并显示图片(缩略图)(图片不存储在mediastore中)
【发布时间】:2012-08-30 05:01:19
【问题描述】:

我最近使用媒体查询和媒体存储设计的自定义图库对显示图片进行了一些测试...效果很好,但我真的需要做一些自定义的事情。

我不希望图片被扫描或在媒体商店中可用,因此我想让我的应用扫描目录并创建缩略图并显示这些缩略图。

我发现很难找到任何高质量的例子来做到这一点。

谁能帮忙举个小例子。

这就是我想做的事情。

  1. 图片存储在 sdcard 的目录中。
  2. 使用我的自定义库,它会扫描此目录,但“不”使用媒体存储
  3. 我需要显示目录的内容,但作为缩略图我想我需要先创建这个缩略图?
  4. 单击缩略图将显示我的自定义图库中的全屏图像。

我想我只需要一点帮助来从目录中获取图片,因为没有存储 int eh mediastore,所以我不能使用查询。我担心的另一件事是我需要为这些图像中的每一个创建缩略图(即时??)因为显示图像但尺寸减小,我怀疑这对性能非常不利。

谁能伸出援助之手?

提前致谢

【问题讨论】:

    标签: android android-gallery


    【解决方案1】:

    不久前我也做过同样的事情。您必须将图像所在的文件夹名称传递给setBaseFolder。这个方法反过来调用refresh(),它使用FilenameFilter(代码不包括但很容易实现)从该文件夹中获取所有名为orig_....jpg的图像并将其保存在mFileList中。然后我们调用notifyDataSetChanged(),这反过来会为每个单元格触发getView()

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

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

    public class PhotoAdapter extends BaseAdapter {
    
    private Context mContext;
    private int mCellSize;
    private File mFolder;
    private File[] mFileList;
    private Map<Object, Bitmap> mThumbnails = new HashMap<Object, Bitmap>();
    private Set<Object> mCreatingTriggered = new HashSet<Object>(); // 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<Void, Integer, Bitmap> {
    
        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;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2012-03-13
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      相关资源
      最近更新 更多