I'm wanting to add a large amount of images to an image array and I'm wondering if there is a way to add all the images in a specified folder rather than adding all of them manually in big list.
As far as I know what you want this should make a job:
File folder = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/folderWithImages";
File list[] = file.listFiles();
This will give you array of files in directory. Only I dont remember that adding slash is necessary.
Related
At, first i search in Stackoverflow and internet and find many answers but when i try these answers but no answers can solve my problem. in my project i create a directory that name is files.
Picasso.with(MainActivity.this).load("file:///files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
or
Picasso.with(MainActivity.this).load("/files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
or
File f = new File("files/img_4");
Picasso.with(MainActivity.this).load(f).error(R.drawable.eboss).into(imgArticle);
or
Picasso.with(MainActivity.this).load("file:/files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
but nothing work and i only see error image.
You can load image from files too. Picasso allows that.Here is that from Picasso.
RESOURCE LOADING
Resources, assets, files, content providers are all supported as image sources.
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
load(new File(...)) - BUT File here must be the ones that are created in /data/data/package.name/...
So either copy file to assets or specify path to /data/data/package.name/... or from sdcard. See this to know how to load from sdcard. You can create a file here using getFilessDir().
I think there is no files/ folder in android project structure. You may like to take a look at Managing Projects Overview.
Picasso will load images from the sdcard on your device, not from your AndroidStudio project. What you could do is to move your picture in the drawable-nodpi folder (create it if it does not exist yet), and then you can load the picture like
Picasso.with(context).load(R.drawable.img_4).error(R.drawable.e;boss).into(imgArticle);
If you want to put the file as part of assets then your directory should be called assets and not files. You can read more here
Based on the comments on your question I think the reason you want to use the "files" folder is because you want to pick the image by name retrieved from the database.
The same is very well possible if the images are still kept in the drawable folder which is generally the case.
You can get the drawable resource using the name picked from the database as follows:
Drawable d= DrawableManager.getDrawable("img_4.png");
This will lead to less load on processor in lookup and reading from storage and less code.
And if you must get the resourse id by the image name use this:
int drawableResourceId = this.getResources().getIdentifier("drawableName", "drawable", this.getPackageName());
and then pass the integer id instead of R.drawable.drawableName
I have text files in the raw folder that I would like to use in my app. I am using ListFragments and depending on what item is selected I want to load that files text in a new layout.
I can properly load the text fine but I would like to be able to scan the file previously so that I can have the first line of the file be the title that is displayed in the ListView. I have a method that determines the number of files in the raw folder and adds the names to the ArrayList.
Here is my method:
private void listRaw() {
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
myArrayList.add(fields[count].getName());
}
}
The problem i am having is setting up a scanner. I do not know how to tell the scanner to scan this specific file. I tried making a new File object and used the Scanner(File) but I do not think I am declaring the file name properly nor if this is even the best way to get this done. Normally if you know the file name you can just simply do:
Scanner fileReader = new Scanner("filename");
but in this loop I never actually have the file name set.
I have text files in the raw folder that I would like to use in my app
I am assuming that "the raw folder" means the res/raw/ resource directory in your project.
I do not know how to tell the scanner to scan this specific file
That is because there is no file. The file exists on your development machine. The representation of the raw resource at runtime is just that: a raw resource. Under the covers, it's effectively an entry in a ZIP file.
You need to get the R.raw value for a raw resource, then use getResources().openRawResource() to get an InputStream that you can pass to a Scanner.
Unless you have different editions of these raw resources for different configurations (e.g., language), you might find it easier to work with the assets/ directory and AssetManager for packaging text files with your app.
In my app am creating some files and storing them in cache dir. now from app only I want to access this files so i get them in a ListView by giving the path getExternalCacheDir().getAbsolutePath(). Here, I get a ListView with the whole path of the files. Instead I just want the file name to be displayed in list.
Can someone help?
Simply call list() in the diretory to return an array of the file names in that directory:
File directory = getExternalCacheDir();
String[] filenames = directory.list();
Alternatively, you can call listFiles() to get back a File[] if that if more useful to you.
HTH
I'm stuck in something that I'm sure rather easy
I have a field in the database that I stored the image name in it, ex 'images/ex1.png'
how can I read this into an imageView?? I tried some code in there but can't get it to work
a good example is highly
You cannot set the image file directly as the resource for your ImageView (that only takes the resource ID).
Instead you will have to use a BitmapFactory to decode it and then use setImageBitmap
EDIT :
Your problem is that there is no way for you to map the name of the image file with the resource ID in the code. The easier way is probably for you to put together a map table.
First put your image files in the drawable folder (res/drawable/ex1.png...) and then, do something like this in your code :
Map map = new HashMap();
// Add key/value pairs to the map
map.put("images/ex1.png", new Integer(R.drawable.ex1));
map.put("images/ex2.png", new Integer(R.drawable.ex2));
// do that for all the images....
String image_name = // query to your database
ImageView the_image_to_change = // findViewById(...)
if (map.containsKey(image_name)) {
the_image_to_change.setImageResource((Integer)(map.get(image_name)).intValue()); // not sure so much is necessary...
}
If you're storing this in your /res/ folder, you need to use a ContentProvider in order to get access to this resource. You shouldn't be accessing resources directly unless you're downloading them externally from the app.
Hope this helps.
myImageView.setImageBitmap(BitmapFactory.decodeFile(myFilePathString));
Your issue is that your file path does not include the root of the device's file system. You have to add this to the beginning of your file path string. This depends on how you save the images. So...
To acquire the appropriate file path to a file on the sd card:
File sdCard = Environment.getExternalStorageDirectory();
String filePath = sdCard.getAbsolutePath() + imageFilePath;
If you're saving your images to internal memory, your app is automatically given a folder to save files to and they will be located there. To find the root to that path use getFilesDir(). For more information on data storage check out http://developer.android.com/guide/topics/data/data-storage.html.
Use the res/drawable folder. Here's how to load an image from drawable to a Bitmap.
Bitmap myImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_image);
More on resources: http://developer.android.com/guide/topics/resources/providing-resources.html
I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.