I am having urls of images.
I am creating .JPEG files of it and saving in a directory of SD card.
After downloading all images (creating bitmaps and compressing in JPEG), i am calling another intent, which is supposed to show all images downloaded in that directory.
but I am seeing blank.
I cheecked DDMS, and found jpeg of correct sizes have got downloaded in my desired directory. But my intent is not showing.
To my surprise, when I close emulator , restart it, re run program with another set of image links, now my intent is showing previously downloaded images ! (not the images which r downloaded in this run of program).
Following is code i use for showing downloaded images:
Can anybody help me with this ?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagechooser);
Bundle bundle = this.getIntent().getExtras();
thumbNailsLnkdhs = (LinkedHashSet<String>) bundle.getSerializable("keyThumbNails");
Toast.makeText(getApplicationContext(),
"ThumbNailsLnkdhs size in new intent: "+thumbNailsLnkdhs.size(), Toast.LENGTH_LONG).show();
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor=managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%myDesiredDirectory%"},
null);
Log.d(LOGGER, "cursor made");
int colcount=imagecursor.getColumnCount();
//.d(LOGGER, "cursor column count is: "+colcount);
int count =imagecursor.getCount();
//Log.d(LOGGER, "count is: "+count);
String colnAME=imagecursor.getColumnName(0);
//Log.d(LOGGER, "COL NAME FOR 0TH COLUMN="+colnAME);
String colnAME_1=imagecursor.getColumnName(1);
//Log.d(LOGGER, "COL NAME FOR 1st COLUMN="+colnAME_1);
int image_column_index = imagecursor.getColumnIndex(imagecursor.getColumnName(1));
//Log.d(LOGGER, "image_column_index="+image_column_index);
//Log.d(LOGGER, "ThumbNailsLnkdhs size: "+ThumbNailsLnkdhs.size());
//int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
//Log.d(LOGGER, "this.count="+this.count);
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Log.d(LOGGER, "dataColumnIndex="+dataColumnIndex);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();
}
ok, so as per suggestion of below ansr,
I found I need to insert image in gallery after I create its physical file.
I tried
MediaStore.Images.Media.insertImage(getContentResolver(), bitmapimage,
imgname+ ".jpg Card Image", "imgDescription"
+ ".jpg Card Image");
but couldnt use getcontentresolver as im in a class which downloads,creates physical file and not inn activity.
So I tried
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+Environment.getExternalStorageDirectory()
+ "/myDesiredDirectory/")))
in my activity which displays previously downloaded images ... but it didnt helped me to show newly downloaded images...
so tried
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory()+)));
this also not showing new images :(
Can anyone help me with this ?
when you put Image manually in AVD through DDMS, you need to scan media from settings right?
same problem here
After download you need to scan media via coding and close the image cursor before set adapter.
You are creating a new ImageAdapter and then attaching the GridView to that 'new' adapter. In other words your GridView is bound to be empty as it is attached to a new blank adapter.
Where are you connecting the Bitmaps/Cursor to the Adapter/View?
Related
I am populating a gridview with MICRO_KIND thumbnails using the following:
/* Find images of interest */
imagecursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CON TENT_URI,
columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[]{"%/houseTab" + currentHouseNumber + "/%"},
null);
/* Retrieve MICRO_KIND Thumbnails */
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getActivity().getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
The retrieve process works perfectly; the issue happen when I delete the actual image files I can not delete the MICRO_KIND Thumbnails. This is what I am using right now and the files images gets deleted but the MICRO_KIND does not get deleted and still visible in the gridview even after a refresh. To get rid of the thumbnail I have to turn off the device or do a unmount/mount of the sdcard.
int count = imagecursor.getCount();
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
for (int i = 0; i < count; i++) {
new File(arrPath[i]).delete(); // Delete the actual image file
imagecursor.moveToPosition(i);
long id = imagecursor.getInt(image_column_index);
/* Delete the thumbnails ???? Not working */
cr.delete(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, MediaStore.Images.Thumbnails.IMAGE_ID +
"= ?",new String[]{"" + id});
By the way arrPath is retrieve from the mediastore using the following:
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
I also try to following to delete the thumbnails but also without any success.
MediaScannerConnection.scanFile(
getActivity().getApplicationContext(),
new String[]{arrPath[i]},
null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
refreshImages();
}
});
So how do I remove this entry from the database so the when the imagecursor is refreshed after the file deletion the imagecursor is empty and no MICRO_KIND or any data for that matter is returned???
Any help would be appreciated.
Hopefully this will help others. I manage to delete the entry from the mediastore hence the MICRO_KIND thumbnails using the following:
Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
id);
cr.delete(uri,null,null);
I'm having this same problem, I couldn't find any way to delete the MICRO_KIND thumbnails no matter what I did, so I peeked through the MediaStore source code and saw that there were references to the /sdcard/DCIM/.thumbnails/.thumbdata3--##### file, and it looks like thats where the MICRO_KIND thumbnails are stored.
The MediaStore database holds records that point to the thumbnail files in the /sdcard/DCIM/.thumbnails folder, so when you delete a thumbnail from MediaStore, it also deletes the file under that folder (which appear to be the MINI_KIND thumbnails since they are considerably larger than MICRO_KIND).
When I deleted the /sdcard/DCIM/.thumbnails/.thumbdata--BLAHBLAH file through ADB, it fixed my problem, the correct thumbnails loaded and I noticed the file instantly was recreated.
I also noticed when I deleted the file throught he ContentResolver like you did, and then queried the MINI_KIND thumbnails, they always loaded correctly.
So my answer (which seems more like a workaround than an answer) was to query the MINI_KIND thumbnails from the MediaStore, and extract a thumbnail from that:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 3;
Bitmap out = MediaStore.Images.Thumbnails.getThumbnail(contentResolver, imageId, MediaStore.Images.Thumbnails.MINI_KIND, opts);
out = ThumbnailUtils.extractThumbnail(out, 96, 96);
And delete the thumbnail using user2553585's answer
Hope this helps
aI am having a problem in displaying all the videos inside a folder in SD card. Currently, I am able to display all the videos that can be found in SD card, but what I am trying to do is to display all the video inside the "PartyVideo" folder inside the SD card. Can you help me?Below is my code that display all the video in SD card.
final String[] columns = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID };
final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
Cursor imagecursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + " DESC");
int image_column_index = imagecursor.getColumnIndex(MediaStore.Video.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++)
{
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
thumbnails[i] = MediaStore.Video.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Video.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
I tried many different code, and end up with this one. The code below gets all that video in the folder I want but I don't know how can I display it.
String[] fileList = null;
File videoFiles = new File(Environment.getExternalStorageDirectory()+"/PartyVideo");
if(videoFiles.isDirectory())
{
fileList=videoFiles.list();
}
for(int i=0;i<fileList.length;i++)
{
Log.e("Video:"+i+" File name",fileList[i]);
}
Use this File Browser library to browse through files and put the filter of video type in the following code
Intent intent = new Intent(getBaseContext(), FileDialog.class);
intent.putExtra(FileDialog.START_PATH, "/sdcard");
//can user select directories or not
intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
//alternatively you can set file filter
intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "avi" });
startActivityForResult(intent, REQUEST_SAVE);
Just try this hope it will work for you.
http://android-er.blogspot.in/2011/05/display-video-thumbnail-in-listview.html
try this
Cursor videocursor = getActivity().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
columns,
MediaStore.Video.Media.DATA + " like ? ",
new String[]{"%/" + Your folder name + "/%"},
null);
i want to create a custom gallery to display all images and videos(along with duration) in sdcard. i am using the following code to build a custom gallery
Code:
final String[] columns = { MediaStore.Images.Media.DATA ,MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor imagecursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");
this.imageUrls = new ArrayList<String>();
for (int i = 0; i < imagecursor.getCount(); i++) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(imagecursor.getString(dataColumnIndex));
}
String[] parameters = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATE_TAKEN,MediaStore.Video.Thumbnails.DATA};
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
parameters, null, null, null);
for (int i = 0; i < videocursor.getCount(); i++) {
videocursor.moveToPosition(i);
imageUrls.add(videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
}
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.build();
imageAdapter = new ImageAdapter(this, imageUrls);
from the above code i am able to get the path of the video, how can i get the video thumbnail along with video duration. and represent it in the gallery
if there are any buit in projects for custom gallery with videos and images please post the links
i actually want to create a custom gallery to select multiple image and video files. i searched a lot in google i am finding the custom image gallery but not with videos please help me in solving this problem.
You can take idea from Custom GridView with multiple option selection option.
There is an open source project in Github.
https://github.com/paramvir-b/AndroidGridViewCompatLib
in this example you need to change
imageView.setImageResource(mThumbIds[position]);
to
imageView.setImageURI(uri);// URI of Image from SD Card
or
imageView.setImageBitmap(bitmap);
For Video:-
Video Thumbnail is in the form of Bitmap so you can show in ImageView.
private Bitmap bmThumbnail;
private ImageView videoview = null;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(PATH_OF_THE_VIDEO,Thumbnails.MICRO_KIND);
videoview.setImageBitmap(bmThumbnail);
for getting Duration:-
String[] proj = { MediaStore.Video.Media.DATA ,MediaStore.Video.Media.DURATION};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
int column_index_duration = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
cursor.moveToFirst();
long duration= cursor.getInt(column_index_duration);
String videoPath= cursor.getString(column_index);
Getting Video Thumbnails and duration:
for(int ii = 0; ii < videocursor.getCount(); ii ++){
videocursor.moveToPosition(ii);
int id_v = videocursor.getInt(video_column_index);
int datacolumn_v = videocursor.getColumnIndex(MediaStore.Video.Media.DATA);
long duration = videocursor.getInt(video_column_duration); // getting duration of the every videos
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration),
TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)),
TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
durationcount.add(hms);
bitList.add(MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id_v,
MediaStore.Video.Thumbnails.MICRO_KIND, null));
arrPathList.add(videocursor.getString(datacolumn_v));
}
Try this link
Android custom image gallery with checkbox in grid to select multiple items
http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/
int column_index_thumb_data = videoCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA);
String thumbnail = videoCursor.getString(column_index_thumb_data);
The following codes were used to retrieve all photos for the purpose of displaying on gridview. However, if i have 1000 photos, it would result in out of memory error. Is there anyone who could help with this?
P.S. if anyone could, are u able to show the edited codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview
Thanks!!
public void initialize() {
images.clear();
final String[] columns = { MediaStore.Images.Thumbnails._ID };
final String orderBy = MediaStore.Images.Media._ID;
#SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
if(imagecursor != null){
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
images.add(imageItem);
}
//imagecursor.close();
}
notifyDataSetChanged();
}
You're running out of memory because you're loading lots of bitmaps that take up lots of memory. The simple (and correct) answer is: don't load them all at once. Instead you should load each image in the adapter as necessary.
I have an app with two main buttons. One open a gallery with every photo on the device and the other one pops up the camera. Both are working fine but here is the thing: When I take a picture with the camera button it doesn't show on the gallery! Not even if I close the app and open it again (forcing the code for the gallery to run again). The only way I can get the picture to show up is to connect the device to my computer via usb and then disconnect (I think forcing a media search, update, something like that.)
Here is the code for the camera intent:
if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "IMG_" + timeStamp + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 1);
} else {
Toast.makeText(getApplicationContext(), "Seu dispositivo Android não possui uma câmera funcional.", Toast.LENGTH_LONG).show();
}
Here is the code for the gallery:
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
PS: I know that managedQuery is deprecated, but it is the only way I know how to do...
Thanks!
You can try notifying the content URI that there was a change after you write a file:
getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);
I solve my problem after put this code after save my file
// to tell system update data
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));