Android tutorial



hello everyone! my name is oum saokosal. today i'm going to talk about how to cachedata in the internal storage in android. actually i created a library for you. it's gonna be easy to do that and you cansolve a lot of problems like, you can cache


Android tutorial, json data from the internet and store in yourinternal storage and use while there is no internet. you know like, that is offline then you canjust use the data and display the data offline. so first i would like to introduce you tomy youtube channel.


so, this is my youtube channel and subscribefor more video. you can see a lot of materials, some in englishand some in khmer but make sure you find the english one and this is my github. now, to use the file cacher, which is thename of the library, you go to https://github.com/kosalgeek and you go to repositories,you should find file cacher and you can see the tutorial here. so, first download it. so, let's hit download. open it and unzip it.


so, you can see the same call code here andthe library here or you just need the file cacher, just copy and then you go to yourproject and this is my project. and then you go to project here and you willfind apps and libs. so, right click on the libs folder. then, paste it here. ok! then, you right click on this and add thelibrary. and you can go back to android and you cango to gradle too. gradle files and the second one.


you should see the line here, libs/filecacher it means that you added correctly. now you go back to the project in java file. what you have to do is called the file cacherand if you don't know you can follow along so now you want to cache some string. you need to add generic drive like this. make sure that when you read and write youget the correct type. so, this is a string but is not limited tothe three and you can cast arraylist of any


class or maybe hashmap or maybe some text, youknow like, generic type. that's all. and then you can call string cache and youcan write some data here and here you can say some text and you need to cache the narrow, ioexception because sometimes the internal storage may be broken or maybe error, youknow. so you can add your post here, you know that,there's something wrong. so let's put it this way and then if you wantto read it, you can just call read cacher and read it. so, there's another error exception but tomake sure that you read it correctly.


you have to check if the string cacher hascache. so, some time you don't have this. so, you might have some error. test something like this. so for now, i want to return it to a string. because i want to use the law, so i just clicktag here like this and here you can say tag text. so if it is working, then, you gonna see sometext you know in the block below. you can see now you can see some text.


let's say you delete it because imagine youare using offline, you know, internet. as you can see you can still see the textit means that it can stay in your internal storage and it can work in another activityas well. so, let create another activity. choose blank activity and a college sub activityand choose hierarchy here. now, in your sub activity, you can do thesame. first, you create a file cache. then, you call file cacher. so it's context and the form name should bethe same time.


so, it's some text activity. if we use the same file name like this, itmeans that the use the same cache, so be careful if you are using the same name but a differentthing. you can conflict and it can cause your appto not responding. so you had to make sure that this is the samecache that you want to use. now let's call read. i don't have to check because i know but ifyou want to check it, you still can do the same. cache.hascache.


and surrounded by try-catch. let's call string, text again. and in here you have to explicitly give the type stringand then maybe you can do the same for here. so, giving a tag is easy. and if you want to open some activity from here, so in the content made, i have a button then just usemain activity and this is even here so i just call intent in = new intent(mainactivity.this, subactivity.class) and you start activity like this.


this should go to sub activity when you clickon the button. run again. as you can see the main activity is some text. now click on the button and call it sub activity. as you can see you can still see the sometext here. it means that you can cache in some app andyou can use another method for example, if you want to clear cache, you can do it. i just called clear cache. it doesn't need any parameter here.


call it. on main activity, you can see that it's nocache here and if you go to sub activity, there is no cache. and one more thing that i really want to shareis that because you use the same file maybe you are confusing and you accidentally useit and it press a string and then in the sub activity maybe you use like, arraylist forexample, you do something like this it's the same file but a different tag. and let's say, writecache, some text ok! you thought that okay, you get the same asthey are and because you don't know the type


and you may accidentally use like this andyou will see something happen. now in the main, you see some text again herebut let's go to the sub activity. as you can see now that is broken. if you go to sub activity you will see thetext here and it says, classcastexception. then it means that you cast to the wrong class. so, i recommend you to cache another catchwhich is, classcastexception. you do like this and you might add your codehere. then the app will not be broken and you cansee the message here instead. let's go to the sub activity.


now you can see the app not broken but yousee the message here. so you can handle a message yourself. close the cache, see you know they said thatthe string cannot be casted. so, just do it yourself. and if you want to see the whole cast, youcan call the file cast manager. you can see that you call the code down below. just copy. so what it does, is that you will get contextof your app and you can display your path. you can change your files, maybe one or morefile exists or not.


and you can retrieve all the name of the filesand let's do the log here. let's run it. now you'll see that time of year so it isin there in your app and files folder and file name is some text dot txt if you havemore than one file, then you can see more files here and you can delete all the filesas well. if you want to, you call manager and deleteall file like this but it will be gone, all of files will be gone. so make sure that you want to delete all offile otherwise don't use it because this cache will be deleted while your application deletesit.


if you think it has a lot of cache, so youdo delete it yourself.


Android tutorial

if not, just leave it there. so that's all for the video. so make sure that you subscribe my channeland i will see you in the next video. thank you!


Android tutorial Rating: 4.5 Diposkan Oleh: PaduWaras