Android: scanning the catalog and displaying images (thumbnails) (images are not stored in the mediator)

I recently conducted several tests with images using a special gallery that I developed using media queries and a media test ... It did a great job, but I really need to do something.

I don’t want the photos to be scanned or available in the mediator, so I would like the application to scan the catalog and create thumbnails and display these thumbnails.

I find it really thin on earth to find good quality examples for this.

Can anyone help with a little example.

Here is what I want to do.

  • Images are saved in the sdcard directory.
  • Using my own gallery, she scans this directory, but does NOT use the media server
  • I need to display the contents of the directory, but as thumbnails, I assume that I will need to create these thumbnails first?
  • By clicking on thumnail you should see a full screen image from my own gallery.

I suppose I just need a little help getting the images from the directory, given that it does not have any saved media, so I cannot use the request. Another thing that concerns me is that I will need to create thumbnails for each of these images (on the fly ??), because to display images, but with a reduced size, I suspect it will be very bad for performance.

Can anyone lend a helping hand?

Thank you in advance

+4
source share
1 answer

I did the same a while ago. You must pass the name of the folder where your images match setBaseFolder . This method, in turn, calls refresh() , which - using FilenameFilter (the code is not included, but very simple to implement) gets all the images with the name orig_....jpg from this folder and holds it in mFileList . Then we call notifyDataSetChanged() , which in turn calls getView() for each cell.

Now, in getView() , we either retrieve the bitmap image of the thumbnails from the cache, if we already have it, otherwise we will create a gray placeholder and start creating a ThumbnailBuilder to create thumbnails accordingly. get a bitmap.

I think you will have to change ThumbnailBuilder bit, because I create quite large “thumbnails” (500x500), since I need modified images for other purposes. In addition, when I work with photographs taken by the camera, there is something that rotates the image in accordance with exif information. But basically, ThumbnailBuilder just checks if there is already a miniature image (my thumbnails are placed in one folder, but have the small_ prefix instead of orig_ ) - if the thumbnail already exists, we get it as a Bitmap and executed, otherwise an image is created. Finally, in onPostExecute() bitmap is set to 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; } } } 
+2
source

All Articles