Open all image from specific folder in Android default gallery - android

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

Related

Import PDF in my xamarin shared code application

I want to import pdf in my xamarin android project.
I want to have a pdf in my application folder at the installation of the application.
Bu when i try to add this pdf in android ressources and try to open it, i get a path error.
i save my pdf in draxable ressource and call path like : #drawable/pdfname or pdfname. the two solution don t work.
Best regards,
ok, i put it in the asset folder. I want to send email with attachement. I try this but the app said me the file is empty. var file = new Java.IO.File("Assets/CV_2017.pdf"); var uri = Android.Net.Uri.FromFile(file); i set my ressource as "build as android asset".
To get the url of the file in assets folder, you can code for example like this:
var file = new File(Android.Net.Uri.Parse("file:///android_asset/CV_2017.pdf").ToString());
if (file.Exists())
{
var uri = file.AbsolutePath;
}
If you want to access the stream of the file under assets, you can use AssetManager and code for example like this:
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("CV_2017.pdf")))
{
//TODO:
}

how to get directory of images to access via code

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

Camera Albums Directory in android devices

My android app (created by air) saves the token shots at /DCMI/ address but it dosent show them in gallery albums.
How can I find androids photo album directory address so I can save the pictures in them and enable them to be shown in the photo galley
this is my code :
var directory:String;
if (rad1.selected){
directory = "mnt/sdcard/DCIM/SHEKAR/";
}
else {
directory = "/DCIM/Camera/";
}
var savePath:String = directory;
i need replace cameraroll directory to "/DCIM/Camera/" tanx .
Try this:
http://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DCIM
Also, make sure that there's no .nomedia file in your directory or one of the parents.

load Image from specific folder on the sdcard?

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.

Android, how do can I get a list of all files in a folder?

I need the name (String) of all files in res/raw/
I tried:
File f = new File("/");
String[] someFiles = f.list();
It looks like the root directory is the root of the android emulator...and not my computers root directory. That makes enough sense, but doesn't really help me find out where the raw folder exists.
Update: Thanks for all the great replies. It appears that some of these are working, but only getting me half way. Perhaps a more detailed description will aid
I want to get all the mp3 files in the raw folder so I can get all the names, then add them to a URI to play a random MP3 in the following way...
String uriStr = "android.resource://"+ "com.example.phone"+ "/" + "raw/dennis";
Uri uri = Uri.parse(uriStr);
singletonMediaPlayer = MediaPlayer.create(c, uri);
When I put "dennis.mp3" into the assets folder, it does show up as expected, however, using the above code, I can't access that MP3 anymore, unless there is something along the line of:
String uriStr = "android.assets://"+ "com.example.phone"+ "/" + "dennis";
Uri uri = Uri.parse(uriStr);
To list all the names of your raw assets, which are basically the filenames with the extensions stripped off, you can do this:
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
}
}
Since the actual files aren't just sitting on the filesystem once they're on the phone, the name is irrelevant, and you'll need to refer to them by the integer assigned to that resource name. In the above example, you could get this integer thus:
int resourceID=fields[count].getInt(fields[count]);
This is the same int which you'd get by referring to R.raw.whateveryounamedtheresource
This code will retrieve all the files from 'New Foder' of sdCard.
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "New Folder");
for (File f : yourDir.listFiles()) {
if (f.isFile())
{
String name = f.getName();
Log.i("file names", name);
}
}
and also make sure to add android sd card write permission in your manifest.xml file
I need the name (String) of all files in res/raw/
There are no files in res/raw/ on the device. Those are resources. There is no good way to iterate over resources, other than by using reflection to iterate over the static data members of the R.raw class to get the various ID names and values.
but doesn't really help me find out where the raw folder exists.
As a folder, it only exists on your development machine. It is not a folder on the device.
You can use AssetManager:
As far as I can remember, you will have list with (just try different paths):
final String[] allFilesInPackage = getContext().getResources().getAssets().list("");
Look at http://developer.android.com/reference/android/content/res/Resources.html
You can generally acquire the Resources instance associated with your application with getResources().
Resources class provides access to http://developer.android.com/reference/android/content/res/AssetManager.html (see to getAssets() method).
And finally obtain access to your packaged (apk) files with AssetManager.list() method.
Enjoy!
From a Context, you can call something like this:
getResources().getAssets().list("thePath");
link to documentation

Categories

Resources