Load Local Images from Local XML - android

I am loading a XML Parsing files and getting its data on a listview. I want to know is there any way to load images from Local SD Card or application instead of loading it from the Internet URL.
XML file i have like this :-
<id>4</id>
<title>Lesson</title>
<artist>English</artist>
<duration>4:23</duration>
<thumb_url>https://abc.com/apple.png</thumb_url>
Want it to change like this :-
<id>4</id>
<title>Lesson</title>
<artist>English</artist>
<duration>4:23</duration>
<thumb_url>R.id.apple</thumb_url>

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

Android: Populate ListView from an internal XML

I found this tutorial which makes exactly what I need :
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
The problem is that the ListView gets populated from an XML found out on an URL. What I have to change so I can load the list items (Image+Text) from an internal XML ?
What do you mean by "internal" xml?
Is your XML file on the SD card or is it compiled in to your application as a raw resource?
Take a look at these links :
Reading an .xml file from sdcard
Android : Reading XML from local resource (for testing)
The line in your code that you will need to change is :
String xml = parser.getXmlFromUrl(URL); // getting XML from URL

Android: Writing to XML file in Assets

I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)

Modifying existing XML file present on sdcard

I have been working on android for about 4 months, i am working on a simple application to download a xml file placed on local server to the sdcard. I have done this part successfully, now i want to edit the data present in that xml. To get a more clear picture here is a sample code of my xml file ...
<seekbar>
<value>
50
</value>
</seekbar>
now am reading the value 50 by xml parsing and updating the value of seekbar as 50. Nowi change the value of seekbar to 100 through GUI. So when i click on save button i want that the value 50 should be replaced by 100 in the xml present in the sdcard. I have learnt about the sdcard permission but i am not getting about how to go for this modifying part.. Will i have to parse the whole xml again...??? Please help guys....
You can load the xml into a DOM Object. modify the value or value node and write the new DOM object back to the file.

Categories

Resources