I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.
Related
I want to add images with my app. I see option to add images in res/drawable folder. But I have lot of images. How can I access the directory?
e.g. to access sdcard location I can use
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + AppConstant.PHOTO_ALBUM);
But I have images in drawable folder which will be with app. So how to access directory in android project. So that I can use something like File[] listFiles = directory.listFiles();
Is there better way to save and access images through app?
The solution I can suggest you is to store your images into "Asset" folder in your project and you can easily access all the images at once by following these steps:
Create a folder named "images" in your asset folder.
Copy all your images in that "images" folder.
3.Get your images list like this:
String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
4.Now set the images to your "imageview" like this:
InputStream inputstream=mContext.getAssets().open("images/"
+listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);
you can access all you images in drawable folder with :
getResources().getDrawable(R.drawable.name_of_drawable)
and also if you want access or load many images , you can use same names ( with postfix / prefix ) and load theme in a loop easily
My requirement is to open all images and video's of specific folder.
I have refereed this link, Now I am able to show a image in gallery but I want to show all images from a specific folder. Almost all link I have tried on stack but did not get success.
You can set path in File object initialize at that time.
File folder = new File("/sdcard/Photo/"); in this tutorial default path is /sdcard/photo/ at this place you can set your path then get your files.
I sinc it is not good idea but you may write your own searcher in all files on mobile phone for example
public ArrayList<File> getAllphotos(String path){
ArrayList<File> photoPath = new ArrayList<>();
File yourDir = new File(path);
for (File f : yourDir.listFiles()) {
String mas[] = f.toString().split("\\.");
if(mas[mas.length - 1].equalsIgnoreCase("png") || mas[mas.length - 1].equalsIgnoreCase("jpeg")){//or other formats
//it is picture
photoPath.add(f);
}
}
return photoPath;
}
call this wis iternal and external storage
I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.
i have a subfolder in the Assets folder called images where i store my images(of course) :) The things is that i want to get the name for the images which i'm getting but the problem is that i'm also getting other unknown names like: "android-logo-mask.png" which i guess are android's default images. Is there a way i can skip this "android default images" to get only the names of my images? My plan is to save this names in a database to use it as reference for showing the images later on an ImageView. Is it a good idea to use the image name to show the images? Here is some code if it needs:
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
ImageHelper i = new ImageHelper();
i.readImages(context);
}
public class ImageHelper {
public ImageHelper(){}
public void readImages(Context context){
AssetManager am = context.getAssets();
try {
String[] getImages = am.list("images");
for(String imgName : getImages){
Log.e("IMAGE NAME----->", imgName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks in advance.
Use some other name for images folder like "MyImages". It should work fine then.
"images" should be a path like "/path/assets/images/image.png"
It is very interesting. Even if you don't have an assets/images directory, when you use:
myImageList = Arrays.asList(getResources().getAssets().list("images"));
you'll see android-logo-mask.png and android-logo-shine.png listed in the results.
That led me to wondering where those actual image files live in the Android code. I don't know the code base at all, but:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/assets/images/
lists those two in an assets directory under res. (Different from the assets directory where I store my project assets, that's not under res).
Being required to provide a path to an individual file for the AssetManagers list method (which returns an array) just doesn't make any sense. And the method documentation says:
Return a String array of all the assets at the given path.
It makes me wonder if there's an error in the list method, or if there was some other reason it was designed to return particular system assets when an asset list is required.
In a similar fashion, if I use:
myAssetList = Arrays.asList(getResources().getAssets().list(""));
I see "sounds" and "webkit" included, which don't correspond to any existing subdirectory of my assets.
If it occurs that the system adds files in this "images" folder, why not create a new folder for your images that you are sure to contain only your files ?
AFAIK accessing thumbnails for images via MediaStore.Images.Thumbnails would generate thumbnails at first attempt, and that's what I need to perform against specific location on sd card.
The question is how to make valid URI to content under specific folder?
All answers I can find use just MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI as uri to make managedQuery. And result of it is Cursor that points to all sdcard images, while none examples can be found on how to access only specific folder.
Maybe you could just list files in the directory and parse them to get thumbnails without using content provider. You can use inSampleSize option to get small bitmap not the complete image Strange out of memory issue while loading an image to a Bitmap object.
may be is to late, but for some one will be helpfull
Mihai Fonoage said...
Use something like
File imagesDir = new File(Environment.getExternalStorageDirectory().toString() + "/pathToDirectory");
File[] imageList = imagesDir.listFiles();
for (File imagePath : imageList) {
bitmap = BitmapFactory.decodeStream(imagePath.toURL().openStream());}
Here you have some great tutorial.