Android Open Source - PhotoPicker Image Cursor Mapper






From Project

Back to project page PhotoPicker.

License

The source code is released under:

GNU General Public License

If you think the Android project PhotoPicker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.thuytrinh.photopicker.controller;
/* ww w  .  j a v  a2  s  . c om*/
import android.database.Cursor;
import android.util.SparseArray;

import com.thuytrinh.photopicker.model.Photo;

import java.io.File;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.Images.Media.BUCKET_DISPLAY_NAME;
import static android.provider.MediaStore.Images.Media.BUCKET_ID;
import static android.provider.MediaStore.Images.Media.DATA;

public class ImageCursorMapper {
  private final SparseArray<File> dataFileCache = new SparseArray<>();

  private int idIndex = -1;
  private int bucketIdIndex = -1;
  private int bucketDisplayNameIndex = -1;
  private int dataIndex = -1;

  private Cursor cursor;

  public ImageCursorMapper() {
    this(null);
  }

  public ImageCursorMapper(Cursor cursor) {
    this.cursor = cursor;
  }

  public void setCursor(Cursor cursor) {
    this.cursor = cursor;
    dataFileCache.clear();
  }

  public long getId() {
    if (idIndex == -1) {
      idIndex = cursor.getColumnIndex(_ID);
    }

    return cursor.getLong(idIndex);
  }

  public long getBucketId() {
    if (bucketIdIndex == -1) {
      bucketIdIndex = cursor.getColumnIndex(BUCKET_ID);
    }

    return cursor.getLong(bucketIdIndex);
  }

  public String getBucketDisplayName() {
    if (bucketDisplayNameIndex == -1) {
      bucketDisplayNameIndex = cursor.getColumnIndex(BUCKET_DISPLAY_NAME);
    }

    return cursor.getString(bucketDisplayNameIndex);
  }

  public String getData() {
    if (dataIndex == -1) {
      dataIndex = cursor.getColumnIndex(DATA);
    }

    return cursor.getString(dataIndex);
  }

  public File getDataFile() {
    int position = cursor.getPosition();
    File dataFile = dataFileCache.get(position);
    if (dataFile == null) {
      dataFile = new File(getData());
      dataFileCache.put(position, dataFile);
    }

    return dataFile;
  }

  public Photo toPhoto() {
    Photo photo = new Photo();
    photo.setId(getId());
    photo.setAlbumId(getBucketId());
    photo.setPath(getData());
    return photo;
  }
}




Java Source Code List

com.thuytrinh.photopicker.controller.GroupByBucketIdFunc.java
com.thuytrinh.photopicker.controller.ImageCursorMapperTest.java
com.thuytrinh.photopicker.controller.ImageCursorMapper.java
com.thuytrinh.photopicker.controller.SimpleLoaderListenerTest.java
com.thuytrinh.photopicker.controller.SimpleLoaderListener.java
com.thuytrinh.photopicker.controller.activity.PhotoChooserActivityTest.java
com.thuytrinh.photopicker.controller.activity.PhotoChooserActivity.java
com.thuytrinh.photopicker.controller.adapter.AlbumsAdapter.java
com.thuytrinh.photopicker.controller.adapter.PhotosAdapter.java
com.thuytrinh.photopicker.controller.fragment.AlbumsFragment.java
com.thuytrinh.photopicker.controller.fragment.PhotosFragment.java
com.thuytrinh.photopicker.controller.loader.AlbumsLoaderTest.java
com.thuytrinh.photopicker.controller.loader.AlbumsLoader.java
com.thuytrinh.photopicker.controller.loader.PhotosLoaderTest.java
com.thuytrinh.photopicker.controller.loader.PhotosLoader.java
com.thuytrinh.photopicker.model.PhotoTest.java
com.thuytrinh.photopicker.model.Photo.java
com.thuytrinh.photopicker.module.AppModule.java
com.thuytrinh.photopicker.module.ObjectLocator.java
com.thuytrinh.photopicker.view.AlbumView.java
com.thuytrinh.photopicker.view.PhotoItemLayoutTest.java
com.thuytrinh.photopicker.view.PhotoItemLayout.java