How to scan raw resources? - android

Is it possible to scan the raw resource folder and obtain a list of all the files in it? Or do I have to list all the files I want to use explicitly in my App?
Therefore, is the only possibility to access raw resources with
R.raw.<identifier>
or is it possible to somehow obtain a list of everything in R.raw.*?

I don't think there is a way to list down files from raw resource folder. But if you know the filenames, you can fetch from the identifier as:
int imageResource1 = getResources().getIdentifier("image1", "raw", getPackageName());
imageView.setBackgroundResource(imageResource1);
Where "image1" is the name of some png image file inside Raw folder.
See if this works for you assuming that you have serialised naming to your files, for example, image1.png, image2.png, etc

Related

Can there be a content directory, where I can put files (compile time) and load files from (run time), inside an app?

I have some image files which will be dynamically loaded in my app. I know I can put images in the "/res/drawable" directory, but can there be somewhere else I can put files and loaded them by file name at runtime?
The image files contain characters that are now allowed in resource names. I did not name them, so ideally, I need to keep the names. For example, suppose I have "Cat #1.png" and "Cat #2.png", and I would like to load the image into an ImageView by that name at runtime, like so:
MyImageView1.setImageBitmap(createBitmapFromContentDirectory("Cat Pictures/Cat #2.png"))
MyImageView2.setImageBitmap(createBitmapFromContentDirectory("Dog Pictures/Dog #1.png"))
Is that possible, or should I change all image names into conforming names (e.g., cat_number1.png and cat_number2.png) and put them into the drawable directory?
You can put them in assets & load them from there,
Otherwise you can also put them in raw folder.
you can refer to Where do I I Place assets folder for assets.assets doesn't create Resource Id, so you can access them directly. Have a look at this
difference between assets & raw.

Can't load image from disk with picasso

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

Setting custom name for raw resource

There are a couple of audio mp3 files in res/raw which go as prepacked audio files for the app. They all have proper names ...must contain only [a-z0-9_.] which do not look nice when parsed inside ListView.
I see many apps have their custom names for audio. For example, raw file name is "default1" and custom audio name is seen as "The Morning Shine".
How can I set custom name for all custom audios in res/raw?
If these files are static and prepacked as you say, try having an xml resource file that holds custom Strings for each file.
I don't know how you are managing your List, but with a custom Adapter you can link the proper name based on the resource ID.
Other way that maybe could help you is read the Tags directly from the files, with the Class MediaMetadataRetriever.
Example:
MediaMetadataRetriever retriever=new MediaMetadataRetriever();
retriever.setDataSource(yourFilePath);
String songName=retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
You have other constants like METADATA_KEY_ARTIST, METADATA_KEY_ALBUM and so on for the other fields.
See: http://developer.android.com/intl/es/reference/android/media/MediaMetadataRetriever.html
Don't forget to call retriever.release() when you're done to free memory.
Good luck.

How can I scan through a raw text file?

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.

Where are the resources' IDs?

Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.

Categories

Resources