Breaking News
Loading...
Tuesday 30 October 2012

Get thumbnails (in MediaStore.Images.Thumbnails) associated with MediaStore.Images.Media

07:14
Last exercise demonstrate how to "Get path of image in MediaStore.Images.Media from SimpleCursorAdapter". In this exercise, the associated thumbnails (in MediaStore.Images.Thumbnails) can be retrieved via the Cursor.

Get thumbnails (in MediaStore.Images.Thumbnails) associated with MediaStore.Images.Media


package com.example.androidlistimages;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends ListActivity {

//define source of MediaStore.Images.Media, internal or external storage
final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;

SimpleCursorAdapter mySimpleCursorAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

String[] from = {MediaStore.MediaColumns.TITLE};
int[] to = {android.R.id.text1};

CursorLoader cursorLoader = new CursorLoader(
this,
sourceUri,
null,
null,
null,
MediaStore.Audio.Media.TITLE);

Cursor cursor = cursorLoader.loadInBackground();

mySimpleCursorAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
cursor,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

setListAdapter(mySimpleCursorAdapter);

getListView().setOnItemClickListener(myOnItemClickListener);
}

OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor cursor = mySimpleCursorAdapter.getCursor();
cursor.moveToPosition(position);

int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
getThumbnail(int_ID);
}};

private Bitmap getThumbnail(int id){

String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};

CursorLoader thumbCursorLoader = new CursorLoader(
this,
thumbUri,
thumbColumns,
thumb_IMAGE_ID + "=" + id,
null,
null);

Cursor thumbCursor = thumbCursorLoader.loadInBackground();

Bitmap thumbBitmap = null;
if(thumbCursor.moveToFirst()){
int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);

String thumbPath = thumbCursor.getString(thCulumnIndex);

Toast.makeText(getApplicationContext(),
thumbPath,
Toast.LENGTH_LONG).show();

thumbBitmap = BitmapFactory.decodeFile(thumbPath);

//Create a Dialog to display the thumbnail
AlertDialog.Builder thumbDialog = new AlertDialog.Builder(MainActivity.this);
ImageView thumbView = new ImageView(MainActivity.this);
thumbView.setImageBitmap(thumbBitmap);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(thumbView);
thumbDialog.setView(layout);
thumbDialog.show();

}else{
Toast.makeText(getApplicationContext(),
"NO Thumbnail!",
Toast.LENGTH_LONG).show();
}

return thumbBitmap;
}
}


download filesDownload the files.


Next:
- List Images with thumbnails, implementing custom SimpleCursorAdapter.


0 comments:

Post a Comment

 
Toggle Footer