Android photo gallery not updating with new photos - android

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)));

Related

Why can't I see some of my pictures in the gallery?

I'm trying to read photos existing in the sdcard with MediaStore.Images.Media.DATA but I'm always getting an empty cursor !!!
This is the code I'm using for the reading :
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor imagecursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
MediaStore.Images.Media.DATA + " like ? ",
new String[] { "%"+eventName.trim()+"%" }, null);
imagecursor.setNotificationUri(getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.imageUrls = new ArrayList<String>();
Log.i("imagecursor.getCount()", Integer.toString(imagecursor.getCount()));
for (int i = 0; i < imagecursor.getCount(); i++) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(imagecursor.getString(dataColumnIndex));
}
Of course I added this
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
to the AndroidManifest.xml
I found that the problem was with the Media Scanner so i solved it buy using a simple app called Scan Media witch scan the external storage and then the MediaStore will be able to find the new elements .
i don´t know if this will help you but I is working me
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/yoururl/";
Toast.makeText(getActivity(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
in this example i get images and show in a grid view

Display all the video from a specific folder in SD card in android

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);

Custom Gallery with Images and Videos in android to select multiple items

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);

Out of memory issue Bitmap

getting out of memory issue
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);
count = imagecursor.getCount();
thumbnails = new Bitmap[count];
arrPath = new String[count];
thumbnailsselection = new boolean[count];
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
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();
getting Out of memory error here on bitmap:
Bitmap thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
it crashing in some devices
Please help
If you look at Android training manuals, there are a few documents telling you how to do this: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Personally, I would use Android Universal Image Loader to take care of that for you.
You need to compress the bitmap image using the compress method from the Android bitmap class. I ran into this issue a while back and ended up having to compress the images I was using..
Check out the description of the compress method at the link below. It should be fairly straightforward. You just choose your format from the enum's, the "quality", and the output stream.
Reference: http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

download image and show in activity in android

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?

Categories

Resources