Last active
December 31, 2015 17:49
-
-
Save erishen/8022731 to your computer and use it in GitHub Desktop.
Android Get Local Album Photos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Licensed to the Apache Software Foundation (ASF) under one | |
or more contributor license agreements. See the NOTICE file | |
distributed with this work for additional information | |
regarding copyright ownership. The ASF licenses this file | |
to you under the Apache License, Version 2.0 (the | |
"License"); you may not use this file except in compliance | |
with the License. You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, | |
software distributed under the License is distributed on an | |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
KIND, either express or implied. See the License for the | |
specific language governing permissions and limitations | |
under the License. | |
*/ | |
package io.cordova.hellocordova; | |
import org.apache.cordova.Config; | |
import org.apache.cordova.CordovaActivity; | |
import android.annotation.SuppressLint; | |
import android.content.ContentResolver; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.provider.MediaStore.Images; | |
import android.provider.MediaStore.Images.Media; | |
import android.util.Log; | |
public class HelloCordova extends CordovaActivity { | |
private static String LOGTAG = "HelloCordova"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
super.init(); | |
// Set by <content src="index.html" /> in config.xml | |
super.loadUrl(Config.getStartUrl()); | |
// super.loadUrl("file:///android_asset/www/index.html") | |
getLocalPhotos(); | |
} | |
@SuppressLint("InlinedApi") | |
private void getLocalPhotos() { | |
ContentResolver cr = getContentResolver(); | |
QueryHandler qh = new QueryHandler(cr, this); | |
String columns[] = new String[] { Media.DATA, Media._ID, Media.TITLE, Media.DISPLAY_NAME, Media.WIDTH, Media.HEIGHT, | |
Media.ORIENTATION, Media.LATITUDE, Media.LONGITUDE, Media.DATE_TAKEN, Media.DATE_ADDED, Media.DATE_MODIFIED, | |
Media.SIZE, Media.IS_PRIVATE }; | |
String selection = Media.DATA + " LIKE ? "; | |
String selectionArgs[] = new String[] { "%/DCIM/%" }; //100LGDSC; | |
String sortOrder = Media.DATA; | |
qh.startQuery(1, Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, sortOrder); | |
} | |
@SuppressLint("InlinedApi") | |
public void handleCursor(int token, Cursor cursor) { | |
ContentResolver cr = getContentResolver(); | |
if(token == 1) | |
{ | |
int count = cursor.getCount(); | |
Log.i(LOGTAG, "count: " + count); | |
int index = 0; | |
while(cursor.moveToNext()) | |
{ | |
String data = cursor.getString(cursor.getColumnIndex(Media.DATA)); | |
/* | |
if(!StringUtils.containsIgnoreCase(data, "/DCIM/")) | |
continue; | |
*/ | |
long id = cursor.getLong(cursor.getColumnIndex(Media._ID)); | |
String display_name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME)); | |
int orientation = cursor.getInt(cursor.getColumnIndex(Media.ORIENTATION)); | |
float lantitude = cursor.getFloat(cursor.getColumnIndex(Media.LATITUDE)); | |
float longitude = cursor.getFloat(cursor.getColumnIndex(Media.LONGITUDE)); | |
String date_taken = cursor.getString(cursor.getColumnIndex(Media.DATE_TAKEN)); | |
String date_added = cursor.getString(cursor.getColumnIndex(Media.DATE_ADDED)); | |
String date_modified = cursor.getString(cursor.getColumnIndex(Media.DATE_MODIFIED)); | |
long size = cursor.getLong(cursor.getColumnIndex(Media.SIZE)); | |
int is_private = cursor.getInt(cursor.getColumnIndex(Media.IS_PRIVATE)); | |
int width = cursor.getInt(cursor.getColumnIndex(Media.WIDTH)); | |
int height = cursor.getInt(cursor.getColumnIndex(Media.HEIGHT)); | |
index++; | |
Log.i(LOGTAG, index + ". display_name: " + display_name + ", data: " + data + ", orientation: " + orientation + ", lantitude: " + lantitude | |
+ ", longitude: " + longitude + ", date_taken: " + date_taken + ", date_added: " + date_added + ", date_modified: " + date_modified | |
+ ", width: " + width + ", height: " + height + ", size: " + size + ", is_private: " + is_private | |
+ ", id: " + id); | |
String[] args = new String[] { String.valueOf(id) }; | |
String[] columns = new String[] { Images.Thumbnails.DATA, Images.Thumbnails.KIND, Images.Thumbnails.WIDTH, Images.Thumbnails.HEIGHT }; | |
Cursor c = cr.query(Images.Thumbnails.EXTERNAL_CONTENT_URI, columns, Images.Thumbnails.IMAGE_ID + "= ?", args, null); | |
Log.i(LOGTAG, "c.count: " + c.getCount()); | |
while(c.moveToNext()) | |
{ | |
String thumb_data = c.getString(c.getColumnIndex(Images.Thumbnails.DATA)); | |
int thumb_kind = c.getInt(c.getColumnIndex(Images.Thumbnails.KIND)); | |
int thumb_width = c.getInt(c.getColumnIndex(Images.Thumbnails.WIDTH)); | |
int thumb_height = c.getInt(c.getColumnIndex(Images.Thumbnails.HEIGHT)); | |
Log.i(LOGTAG, "thumb_data: " + thumb_data + ", thumb_kind: " + thumb_kind + ", thumb_width: " + thumb_width | |
+ ", thumb_height: " + thumb_height); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.cordova.hellocordova; | |
import android.annotation.SuppressLint; | |
import android.content.AsyncQueryHandler; | |
import android.content.ContentResolver; | |
import android.database.Cursor; | |
import android.net.Uri; | |
public class QueryHandler extends AsyncQueryHandler { | |
private HelloCordova helloCordova; | |
public QueryHandler(ContentResolver cr, HelloCordova hc){ | |
super(cr); | |
this.helloCordova = hc; | |
} | |
public void startQuery(int token, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) { | |
super.startQuery(token, null, uri, projection, selection, selectionArgs, orderBy); | |
} | |
@SuppressLint("InlinedApi") | |
@Override | |
protected void onQueryComplete(int token, Object cookie, Cursor cursor) { | |
super.onQueryComplete(token, cookie, cursor); | |
helloCordova.handleCursor(token, cursor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment