I am trying to make a simple gallery from an asset folder in my app.
so I created an asset folder, and created inside a directory called 'pics' and put some jpg files inside.
Then I did this to get all the images from the pics inside the assets folder. I wanted to get it by the URI of the assets folder but it's not working, I guess because the URI is wrong:
Uri uriExternal = Uri.parse(getApplicationContext().getResources().getAssets().open("pics").toString());
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "bucket_display_name = \""+album_name+"\"", null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
imageList.add(Function.mappingInbox(album, path, null));
}
Use something like this:
AssetManager assets = getApplicationContext().getResources().getAssets();
String[] pics = assets.list("pics");
ArrayList<Bitmap> bmps = new ArrayList<>();
for (String path : pics) {
bmps.add(BitmapFactory.decodeStream(assets.open(path));
}
The bmps list now contains all the images in your pics folder as Bitmaps.
I wrote this purely from documentation, and I haven't tested it. I recommend you read the documentation yourself if you run into trouble: https://developer.android.com/reference/android/content/res/AssetManager
Related
I want to build a image slider that can shift images automatically with looping all images from phone's gallery. My android device is android 4.0.4. Can anyone help me out?
I have successfully built a image slider that can shift images automatically, but all the images are from the drawable resource that I created using Android Studio. I want the images come from gallery for better user interface.
My code is like this:
int images[] = {R.drawable.slide1, R.drawable.slide7, R.drawable.slide10};
It requires looping with array to do this, but I am a beginner of java.
1) content provider may help. like
private ArrayList<String> getImages(Context context) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = context.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}
loading images from gallery with content provider
also look to the file ways as those are also useful.
listing files
loading images from file path
I'm using org.apache.commons.io.FileUtils to move files from one directory to another, like so:
File source = new File("/my/image/directory/image.jpg";
File destination = new File("/new/image/directory/image.jpg");
FileUtils.moveFile(source, destination);
Note that the source directory is an external directory and destination is an internal directory.
After above code is executed, I query to get the files of my internal directory like so:
File vaultDir = ctx.getDir("dir_name", Context.MODE_PRIVATE);
String[] fileList = vaultDir.list();
//Iterate and print file names
This is working as intended. But when i query the MediaStore like so:
ArrayList<GridViewObject> objects = new ArrayList<>();
String[] mProjection = {MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA};
ContentResolver cr = ctx.getContentResolver();
Cursor mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mProjection,
null,
null,
MediaStore.Images.Media.DEFAULT_SORT_ORDER);
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
GridViewObject tmpGridViewObject = new GridViewObject();
tmpGridViewObject.setTitle(mCursor.getString(0));
tmpGridViewObject.setUrl(mCursor.getString(1));
objects.add(tmpGridViewObject);
mCursor.moveToNext();
}
The image is still returned to me by the query. Also, when I open my camera gallery app, the image is still there, as if it was never deleted.
I noticed that if I restart the phone, the image disappears from my device camera gallery app as intended (and above query return correct result). So I think it might be an indexing problem.
What do I need to do to "update" the MediaStore to show the intended result?
I solved this by instead of deleting the file using FileUtils.delete(srcFile, destFile); i copied the the file using FileUtils.copy(srcFile, destFile); and then to refresh MediaStore i used the following code to delete the source entry:
String selection = MediaStore.Images.Media.DATA + "='" + source.getAbsolutePath() + "'";
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, null);
Credit goes to Pascal's answer to this question.
I want take image from camera then save image in SD card folder but path is save in SqLite database and also save image in binary format. Also when read from database then show image in ImageView or GridView. any have resource or example or tutorial links about this. If anybody give me any resource, or example or tutorial links. It will be very helpful for me.
You can split your task in three independent parts:
1. Take a picture from camera
2. Save it to a file and take the path
3. Store the path from point two in your database
For part 1 and 2:
final int IMAGE_FROM_CAMERA = 0;
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, IMAGE_FROM_CAMERA);
In onActivityResult()...
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String path = getRealPathFromURI(selectedImage);
}
...
public String getRealPathFromURI(Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null,
null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
For part 3 just write the string path the appropriate field in your database. For opening is easy, since you have the path, just open a stream from the path, do some conversions and get your image.
I can get the list of images in sd card using fololowing`
but these are not sorted
how can I get them sorted either by name or time
thanks in advance
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/somedirectoryname/";
File targetDirectory = new File(targetPath);
File[] files = targetDirectory.listFiles();`
for (File file : files1) {
list.add(file.getAbsolutePath().toString());
}
Maybe it will be easier if you use the built-in content provider
MediaStore.Images.Media.query(cr, uri, projection, where, orderBy)
something like this:
String[] projection = {MediaColumns._ID, MediaColumns.DISPLAY_NAME, MediaColumns.DATE_ADDED};
Cursor c = MediaStore.Images.Media.query(getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, MediaColumns.DATE_ADDED);
then you can loop the cursor to do whatever you need.
- Use lastModified() method of File to get the time the file was modified.
- Store them in Collection like ArrayList<File>.
- Use java.util.Comparator<T> to compare and sort them according to time and name.
i am trying to populate listview with video files from a folder created on sd card. I am using
managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,mystring, null, null, null);
But it populates all videos saved in sdcard, but i want only those videos which are saved in specific folder. I have also used
Uri uri = Uri.fromFile(filepath);
cursor = managedQuery(uri, mystring , null , null , null);
and
Uri uri = Uri.parse(filepath);
cursor = managedQuery(uri, mystring , null , null , null);
But it doesn't work. I have tried lot and got help from google still not succeded.
Is there any way to give path of that folder? or any other way?
you can use this code for get videos from specific folder as:
String selection=MediaStore.Video.Media.DATA +" like?";
String[] selectionArgs=new String[]{"%FolderName%"};
videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");
You can get a files form a specific location like this
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File("/sdcard/");
// or you can use File f=new File(Environment.getExternalStorageDirectory().getPAth());
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
hey managedquery search the whole sdcard not the specific folder .
the above method is true of display the name of file but if you to display thumbnail create thumbmail and store in in Hashmap and populate it to the list of gallery adapter..........