I need a sample code or tutorial for accessing phone images/media through content provider ?
I know the following, what nex开发者_如何学运维t ?
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
Using a CursorLoader:
(by implementing LoaderManager.LoaderCallbacks<Cursor>
in your AppCompatActivity
)
private void loadPhotosFromPhone() {
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {MediaStore.Images.Media.DATA};
return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
ArrayList<String> alstPhotos = new ArrayList<>();
for (data.moveToLast(); !data.isBeforeFirst(); data.moveToPrevious()){
String photoPath = data.getString(0);
alstPhotos.add(photoPath);
}
// Use alstPhotos
}
This would load all the Images on the Device, asynchronously.
I made a helper class that will collect all images path from user's device. You can use it if you want. For your info, it might be slow to load the images, so it will be good if you load them via background thread or loader.
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import java.util.ArrayList;
/**
* @author hendrawd on 10/26/16
*/
public class ImagePathProvider {
/**
* Getting All Images Path.
*
* @param context the activity
* @return ArrayList with images Path
*/
public static ArrayList<String> getAllImagesPath(Context context) {
ArrayList<String> listOfAllImages = new ArrayList<>();
listOfAllImages.addAll(getExternalImagesPath(context));
listOfAllImages.addAll(getInternalImagesPath(context));
return listOfAllImages;
}
/**
* Getting All External Images Path.
*
* @param context the context
* @return ArrayList with external images Path
*/
private static ArrayList<String> getExternalImagesPath(Context context) {
return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
}
/**
* Getting All Internal Images Path.
*
* @param context the context
* @return ArrayList with internal images Path
*/
private static ArrayList<String> getInternalImagesPath(Context context) {
return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
}
private static ArrayList<String> getImagesPathFromUri(Context context, Uri uri) {
Cursor cursor;
int column_index_data;
ArrayList<String> listOfAllImages = new ArrayList<>();
String absolutePathOfImage;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
cursor.close();
}
return listOfAllImages;
}
}
精彩评论