Android Open Source - ImageScanner Gallery Folder Activity






From Project

Back to project page ImageScanner.

License

The source code is released under:

Apache License

If you think the Android project ImageScanner 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.example.imagescanner;
//from   www.jav a2 s.c om
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.atermenji.android.iconicdroid.icon.EntypoIcon;
import com.scanner.adapter.GalleryFolderListAdapter;
import com.scanner.beans.GalleryFolderBean;
import com.scanner.utils.BitmapUtils;
import com.scanner.utils.ConstantsUtils;
import com.scanner.utils.FormatBitmapUtils;
import com.scanner.utils.ScannerUtils;

/**
 * GalleryFolderActivity
 * 
 * @date 2014-10-29
 * @author Quinn
 * 
 */
public class GalleryFolderActivity extends Activity implements
    OnItemClickListener, OnClickListener {

  private final static int REDIRECT_2_GALLERYGRID = 0;
  private final static int REDIRECT_2_CAMERA = 2;
  private final static int REDIRECT_2_MAIN = 1;
  private static final String TAG = "GalleryFolderActivity";
  private static final String CACHEPATH = "cameraImage";

  // view widget
  private View backArea;
  private ImageView backIcon;
  private ListView listview;
  private ProgressBar bar;

  // adapter and datalist of listview
  private GalleryFolderListAdapter lvAdapter;
  private ArrayList<GalleryFolderBean> lvData;
  // the index of foloder you choose in listview
  private String chosenPath;
  // cache path of picture from camera
  private File cameraCacheImage;
  private String imgUrl;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.activity_gallery_folder);
    initValue();
    initUI();
  }

  public void initValue() {
    lvData = new ArrayList<GalleryFolderBean>();
    cameraCacheImage = new File(Environment.getExternalStorageDirectory(),
        getPhotoFileName());
  }

  public void initUI() {
    backArea = (View) findViewById(R.id.galleryBack);
    backIcon = (ImageView) findViewById(R.id.galleryBack_icon);
    BitmapUtils.setIconFont(this, backIcon, EntypoIcon.CHEVRON_THIN_LEFT,
        R.color.theme_color);
    listview = (ListView) findViewById(R.id.gallery_folder_list);
    bar = (ProgressBar) findViewById(R.id.gallerry_progress_bar);
    backArea.setOnClickListener(this);
    lvAdapter = new GalleryFolderListAdapter(this, lvData,
        R.layout.item_lv_gallery_folder);
    listview.setAdapter(lvAdapter);
    loadLvData();
    listview.setOnItemClickListener(this);

  }

  /**
   * load listview data
   */
  public void loadLvData() {
    final Handler handler = new Handler() {

      @Override
      public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        if (msg.what == 1) {
          lvAdapter.notifyDataSetChanged();
          bar.setVisibility(View.GONE);
        }
      }

    };
    new Thread(new Runnable() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        ArrayList<GalleryFolderBean> tempList = ScannerUtils
            .getGalleryFolder(GalleryFolderActivity.this);
        // this bean is invalid, it just represents the buttom of call
        // camera
        GalleryFolderBean cameraBean = new GalleryFolderBean(null,
            "take a picture", "", "");
        lvData.add(cameraBean);
        for (GalleryFolderBean bean : tempList) {
          lvData.add(bean);
        }
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);
      }
    }).start();
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {

    if (position != 0) {
      // go into a folder
      chosenPath = lvData.get(position).getFolderPath();
      redirectTo(REDIRECT_2_GALLERYGRID);
    } else {
      // take a picture from camera
      redirectTo(REDIRECT_2_CAMERA);
    }
  }

  public void redirectTo(int dest) {
    // // TODO Auto-generated method stub
    switch (dest) {
    case REDIRECT_2_GALLERYGRID:
      Intent intent = new Intent(this, GalleryGridActivity.class);
      Bundle bundle = new Bundle();
      bundle.putString(ConstantsUtils.BUNDLE_FOLDER_PATH, chosenPath);
      intent.putExtras(bundle);
      this.startActivityForResult(intent,
          ConstantsUtils.REQUEST_FROM_GRIDVIEW);
      break;
    case REDIRECT_2_CAMERA:
      Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      String cameraImgSavePath = getDiskCacheDir(this, CACHEPATH);
      cameraCacheImage = new File(cameraImgSavePath);
      cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
          Uri.fromFile(cameraCacheImage));
      this.startActivityForResult(cameraintent,
          ConstantsUtils.REQUEST_FROM_CAMERA);
      break;
    case REDIRECT_2_MAIN:
      onBackPressed();
      break;
    default:
      break;
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bitmap bitmap;
    if (resultCode != RESULT_OK)
      return;
    switch (requestCode) {
    case ConstantsUtils.REQUEST_FROM_GRIDVIEW:

      if (data != null) {
        imgUrl = data.getExtras().getString(
            ConstantsUtils.BUNDLE_IMAGE_PATH_FROM_GRIDVIEW);
        Intent intent_c = new Intent();
        Bundle bundle_c = new Bundle();
        bundle_c.putString(ConstantsUtils.BUNDLE_IMAGE_PATH_FINAL,
            imgUrl);
        intent_c.putExtras(bundle_c);
        setResult(RESULT_OK, intent_c);
        finish();

      }

      break;
    case ConstantsUtils.REQUEST_FROM_CAMERA:
      imgUrl = cameraCacheImage.getAbsolutePath();
      bitmap = BitmapUtils.compressImageFromFile(imgUrl);
      FormatBitmapUtils.saveBitmap(imgUrl, bitmap);

      Intent intent_c = new Intent();
      Bundle bundle_c = new Bundle();
      bundle_c.putString(ConstantsUtils.BUNDLE_IMAGE_PATH_FINAL, imgUrl);
      intent_c.putExtras(bundle_c);
      setResult(RESULT_OK, intent_c);
      finish();
      break;

    default:
      break;
    }
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == R.id.galleryBack)
      redirectTo(REDIRECT_2_MAIN);

  }

  /**
   * ???????uniqueName?????????????????
   */
  public String getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment
        .getExternalStorageState())
        || !Environment.isExternalStorageRemovable()) {
      cachePath = context.getExternalCacheDir().getPath();
    } else {
      cachePath = context.getCacheDir().getPath();
    }
    File cacheDir = new File(cachePath + File.separator + uniqueName);
    if (!cacheDir.exists()) {
      cacheDir.mkdirs();
    }
    String cameraImgSavePath = cacheDir.getPath() + File.separator
        + getPhotoFileName();
    return cameraImgSavePath;
  }

  // ???????????????????????
  private String getPhotoFileName() {
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat dateFormat = new SimpleDateFormat(
        "'IMG'_yyyyMMdd_HHmmss");
    return dateFormat.format(date) + ".jpg";
  }

}




Java Source Code List

com.atermenji.android.iconicdroid.IconicFontDrawable.java
com.atermenji.android.iconicdroid.icon.EntypoIcon.java
com.atermenji.android.iconicdroid.icon.EntypoSocialIcon.java
com.atermenji.android.iconicdroid.icon.FontAwesomeIcon.java
com.atermenji.android.iconicdroid.icon.Icon.java
com.atermenji.android.iconicdroid.icon.IconicIcon.java
com.atermenji.android.iconicdroid.util.TypefaceManager.java
com.example.imagescanner.GalleryFolderActivity.java
com.example.imagescanner.GalleryGridActivity.java
com.example.imagescanner.MainActivity.java
com.scanner.adapter.GalleryFolderListAdapter.java
com.scanner.adapter.GalleryGridAdapter.java
com.scanner.beans.GalleryFolderBean.java
com.scanner.beans.GridImageBean.java
com.scanner.utils.BitmapUtils.java
com.scanner.utils.ConstantsUtils.java
com.scanner.utils.DensityUtil.java
com.scanner.utils.FormatBitmapUtils.java
com.scanner.utils.ImageLoaderCacheHelper.java
com.scanner.utils.L.java
com.scanner.utils.ScannerUtils.java
com.scanner.utils.UIHelper.java