Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced deprecated method withLoaderManager in CustomGalleryActivity #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="7"
android:minSdkVersion="11"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Expand Down
290 changes: 138 additions & 152 deletions src/com/luminous/pick/CustomGalleryActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.luminous.pick;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
Expand All @@ -20,195 +18,183 @@
import android.widget.GridView;
import android.widget.ImageView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class CustomGalleryActivity extends Activity {
import java.util.ArrayList;

public class CustomGalleryActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{

GridView gridGallery;
Handler handler;
GalleryAdapter adapter;

ImageView imgNoMedia;
Button btnGalleryOk;

String action;
private ImageLoader imageLoader;

LoaderManager loaderManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);

GridView gridGallery;
Handler handler;
GalleryAdapter adapter;
action = getIntent().getAction();
if (action == null) {
finish();
}
initImageLoader();
init();
}

ImageView imgNoMedia;
Button btnGalleryOk;

String action;
private ImageLoader imageLoader;
private void initImageLoader() {
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
.bitmapConfig(Bitmap.Config.RGB_565).build();
ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
this).defaultDisplayImageOptions(defaultOptions).memoryCache(
new WeakMemoryCache());

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
ImageLoaderConfiguration config = builder.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
}

action = getIntent().getAction();
if (action == null) {
finish();
}
initImageLoader();
init();
}
private void init() {

private void initImageLoader() {
try {
String CACHE_DIR = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/.temp_tmp";
new File(CACHE_DIR).mkdirs();
loaderManager = getLoaderManager();
loaderManager.initLoader(1, null, this);

File cacheDir = StorageUtils.getOwnCacheDirectory(getBaseContext(),
CACHE_DIR);
handler = new Handler();
gridGallery = (GridView) findViewById(R.id.gridGallery);
gridGallery.setFastScrollEnabled(true);
adapter = new GalleryAdapter(getApplicationContext(), imageLoader);

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565).build();
ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
getBaseContext())
.defaultDisplayImageOptions(defaultOptions)
.discCache(new UnlimitedDiscCache(cacheDir))
.memoryCache(new WeakMemoryCache());
if (action.equalsIgnoreCase(Action.ACTION_MULTIPLE_PICK)) {

ImageLoaderConfiguration config = builder.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
findViewById(R.id.llBottomContainer).setVisibility(View.VISIBLE);
gridGallery.setOnItemClickListener(mItemMulClickListener);
adapter.setMultiplePick(true);

} catch (Exception e) {

}
}

private void init() {

handler = new Handler();
gridGallery = (GridView) findViewById(R.id.gridGallery);
gridGallery.setFastScrollEnabled(true);
adapter = new GalleryAdapter(getApplicationContext(), imageLoader);
PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader,
true, true);
gridGallery.setOnScrollListener(listener);
} else if (action.equalsIgnoreCase(Action.ACTION_PICK)) {

if (action.equalsIgnoreCase(Action.ACTION_MULTIPLE_PICK)) {
findViewById(R.id.llBottomContainer).setVisibility(View.GONE);
gridGallery.setOnItemClickListener(mItemSingleClickListener);
adapter.setMultiplePick(false);

findViewById(R.id.llBottomContainer).setVisibility(View.VISIBLE);
gridGallery.setOnItemClickListener(mItemMulClickListener);
adapter.setMultiplePick(true);
}

} else if (action.equalsIgnoreCase(Action.ACTION_PICK)) {
gridGallery.setAdapter(adapter);
imgNoMedia = (ImageView) findViewById(R.id.imgNoMedia);

findViewById(R.id.llBottomContainer).setVisibility(View.GONE);
gridGallery.setOnItemClickListener(mItemSingleClickListener);
adapter.setMultiplePick(false);
btnGalleryOk = (Button) findViewById(R.id.btnGalleryOk);
btnGalleryOk.setOnClickListener(mOkClickListener);

}

gridGallery.setAdapter(adapter);
imgNoMedia = (ImageView) findViewById(R.id.imgNoMedia);

btnGalleryOk = (Button) findViewById(R.id.btnGalleryOk);
btnGalleryOk.setOnClickListener(mOkClickListener);
}

new Thread() {
private void checkImageStatus() {
if (adapter.isEmpty()) {
imgNoMedia.setVisibility(View.VISIBLE);
} else {
imgNoMedia.setVisibility(View.GONE);
}
}

@Override
public void run() {
Looper.prepare();
handler.post(new Runnable() {
View.OnClickListener mOkClickListener = new View.OnClickListener() {

@Override
public void run() {
adapter.addAll(getGalleryPhotos());
checkImageStatus();
}
});
Looper.loop();
};
@Override
public void onClick(View v) {
ArrayList<CustomGallery> selected = adapter.getSelected();

}.start();
String[] allPath = new String[selected.size()];
for (int i = 0; i < allPath.length; i++) {
allPath[i] = selected.get(i).sdcardPath;
}

}

private void checkImageStatus() {
if (adapter.isEmpty()) {
imgNoMedia.setVisibility(View.VISIBLE);
} else {
imgNoMedia.setVisibility(View.GONE);
}
}

View.OnClickListener mOkClickListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
ArrayList<CustomGallery> selected = adapter.getSelected();
Intent data = new Intent().putExtra("all_path", allPath);
setResult(RESULT_OK, data);
finish();

String[] allPath = new String[selected.size()];
for (int i = 0; i < allPath.length; i++) {
allPath[i] = selected.get(i).sdcardPath;
}
}
};
AdapterView.OnItemClickListener mItemMulClickListener = new AdapterView.OnItemClickListener() {

Intent data = new Intent().putExtra("all_path", allPath);
setResult(RESULT_OK, data);
finish();
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
adapter.changeSelection(v, position);

}
};
AdapterView.OnItemClickListener mItemMulClickListener = new AdapterView.OnItemClickListener() {
}
};

@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
adapter.changeSelection(v, position);
AdapterView.OnItemClickListener mItemSingleClickListener = new AdapterView.OnItemClickListener() {

}
};
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomGallery item = adapter.getItem(position);
Intent data = new Intent().putExtra("single_path", item.sdcardPath);
setResult(RESULT_OK, data);
finish();
}
};

AdapterView.OnItemClickListener mItemSingleClickListener = new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomGallery item = adapter.getItem(position);
Intent data = new Intent().putExtra("single_path", item.sdcardPath);
setResult(RESULT_OK, data);
finish();
}
};
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
}

private ArrayList<CustomGallery> getGalleryPhotos() {
ArrayList<CustomGallery> galleryList = new ArrayList<CustomGallery>();
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
final ArrayList<CustomGallery> galleryList = new ArrayList<CustomGallery>();
try {

try {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
if (data != null && data.moveToFirst()) {
while (data.moveToNext()) {
CustomGallery item = new CustomGallery();

Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
int dataColumnIndex = data
.getColumnIndex(MediaStore.Images.Media.DATA);

if (imagecursor != null && imagecursor.getCount() > 0) {
item.sdcardPath = data.getString(dataColumnIndex);
galleryList.add(item);
}
}
} catch (Exception e) {
e.printStackTrace();
}

while (imagecursor.moveToNext()) {
CustomGallery item = new CustomGallery();
new Thread() {

int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
@Override
public void run() {
Looper.prepare();
handler.post(new Runnable() {

item.sdcardPath = imagecursor.getString(dataColumnIndex);
@Override
public void run() {
adapter.addAll(galleryList);
checkImageStatus();
}
});
Looper.loop();
};

galleryList.add(item);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}.start();
}

// show newest photo at beginning of the list
Collections.reverse(galleryList);
return galleryList;
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {

}
}