Storing and dynamically loading pdf files from server in node js - android

I am trying to create an app that displays a list of pdf files, and then load the pdf file in a new browser tab / android activity.
The idea is the user will click a button "Lectures" and then will be shown the list of lectures, clicking on any list item will load the specific pdf file.
I currently have a node js mysql back end, for user login and routes to other pages, which is followed up with both a handlebars and android front end.
I am struggling to understand how I can store pdf files so they can be called based on what the user clicks, I want to store the pdf files on a server rather than within my application and then call them when required.
I have seen a method using ejs view engine to render list of files in public folder, which then allows you to do this, but this entails storing pdfs within the application, alternatively I can host the files with my web hosting, still need to figure out how to load them based on user action.
Would appreciate any direction or solution!
Thanks in advance
Mohamed V

Without knowing how you have the DB set I will talk more along the lines of how you would do this and then modify as needed. In most cases you would probably want to use an object store to store the PDF files. This will allow for access on any application.
Your DB should contain a file name of the PDF file, as an example, lets say you table for 'files' is as such
id, filename, name, extension, courceID
Here you would have the filename maybe the extension type and a refrenceID
Next, in your response callback, you simply return the query for the files, for example let say you run a query like SELECT filename, name WHERE courceID = '12345'
You then return this to the view, if its ejs then you would just pass the object and do something like
<% for ( in in files ) {%>
<div><a href="url-to-object-store/<%-files[i].filename%>"><%-files[i].filename%>
<%}%>

Related

How to save images from server and reuse later

I have an Application and that is live on google, but Now we have plan to change its little working to make it less in size. At the time now its about 60mb and its just due to number of images we have packaged in. Now We have decided to send less embed images in the app built and later let the user download these manually from the server. But I have confusion in couple of things. and these are as following.
How to download the images from server and save them in the device to reuse purpose. I mean let say we have uploaded 10 images on server, user download all of them , now next time if our server has no new images the last 10 images which are saved in devices should be able to reuse and should not be download by the app again.
so this point has three more point
1) How to download and where to download the images
2) If the user has deleted some images downloaded then how to check which images are deleted and to re download them.
3) Is there any way to save the images in a place where user can not see them out side app and not be able to delete them (to eliminate my second point)
Also I have an grid view I am using them to show the image in grid , user click on the image and the real images open in new activity with some info now I have confusion in this also and that is
As we are planning to embed at least 10 images in apk , so we need to show them in the grid (its easy as I am showing them from the app resource folder into the grid view ). But now as we have to download the images from server also , so now How do we populate the images into that grid view and how do we keep track that which image is coming from where either from the resource (as in previous version ,I have just to get it from drawable. but now the things are changed and complicated)
so in short the grid view is mix up of the local app images and the images from the server.
would not it be time consuming to search the last downloaded images from the device and them to display them into a grid view ?
Thanks for reading so long question.
I will try to make it as much clear as possible in step by step:
Create a table in local db (sqlite) and put its name some thing like "Image Paths" with 3 columns, "image_id" , "image_path", "image_name"
When your app launches for the first time after being installed, check if table is empty or not by doing something like this:
// this will return true if the table has no data
public static boolean checkTable(String q){
DataBaseHelper dbh = new DataBaseHelper();
SQLiteDatabase sqldb = dbh.createAndOpenDB();
Cursor cd =null;
try{
boolean isEmpty=false;
cd = sqldb.rawQuery(q, null);
if(cd!=null){
cd.moveToFirst();
if(cd.getInt(0)==0){
isEmpty = true;
}
}
cd.close();
dbh.closeDB();
return isEmpty;}
catch(Exception e){
e.printStackTrace();
if(cd!=null){
cd.close();
dbh.closeDB();
}
return false; } }
If the table is empty then it means either the app is running the first time or the user has deleted the private application data, either way after that you will want to download the whole list of images from server.
When you download images to the device now you should create a separate folder in internal memory which should be private to your app only, to do that:
// write images privately
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
You should then save the name of each image along with its access path to the table that we just created in step 1.
So now accessing those images will be faster and much reliable, if for example you want to update the images from the server. Lets say you have some new images which you want to be synchronized with the device then you should make another table of versions which must be synchronized with the server's version table.. (both must have the same table) So when you check your image_path table in step 2, if the data is found then you should get a list of current versions from the server and match them with your local copy of it. If any version doesn't match then you should update accordingly.
If you want the user to not be able to delete your app's data using the clear data button, then you can hide it using the android:manageSpaceActivity attribute in the manifest file for example:
android:manageSpaceActivity="com.main.slash.LaunchingActivity"
EDIT
After your comments, to create a folder in internal memory privately and save a file in it:
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory/folder;
File imageFile1 = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(imageFile1);
// to get the path of the file you can call the getPath() or getAbsolutePath() methods
String path = imageFile1.getAbsolutePath();
Get images by parsing in bitmap format and store them in your app database, or sdcard.
I think you have downloaded the images, then you should go for saving images to your sdcard memory, with numbering so that you can check them in later launch either present or not.
If not present then download the same image and save to that location with same name.
eg: image_1, image_2, .....image_10. (downloaded images to sd card)
In Activity or wherever you want check if image_1 not present then download and write it with the same filename(file path).
Checking from the sdcard will not consume so much time, wherever you use them like in you GridView.
check link for if need, how to save image from urlto sdcard(external storage):
Android - Save image from URL onto SD card
link to show images from the sdcard to GridView:
http://android-er.blogspot.in/2013/10/gridview-example-load-images-to.html
That's all folks.
Thanks

Need a way to save a meteor.collection as a text file on a mobile

a newbie to Meteor, but I have created under localhost a simple passenger counter I intend to use on an android app for a passenger survey (holding a laptop in an airport isn't a particularly wise idea). It works, but there is no export function. Basically uses meteor.collection as simple rows of data - one row been one passenger with a date/time stamp, two buttons - Add Passenger and Reset. I have tried looking everywhere for a simple answer, but what I want to do is now add to the client side (on browser - but ultimately on the mobile) a button called export, and when clicked, takes each row in the meteor.collection and export it to a text file - line by line. Very simple. Alas right now I haven't got a clue how to proceed. Seen some references to FS.collection, but don't think there is what I want. This is ultimately for a mobile application.
Thanks.
JSON.stringify each object into a larger object of pure text, say bytes
Create a blob from that text object, ex: blob = new Blob([bytes]);
Use the filesaver package to save to a file locally.

Android Save EditText with Image

Right now I have a simple App that takes a picture and saves it to a folder. I'm trying to figure out a way to have a user answer questions about each picture (I would just be using EditTexts with that). Is there a way to connect those EditTexts with the image so that when I would open up the image on another page that i could click a button to view the information that was entered along with that image? I have an idea on how to code everything else, just not keeping the edittexts associated with the image. Any ideas would be greatly appreciated!
Thank you!
perhaps you could create a data file alongside the image that has the same name as the image file, but a different extension.
So if you are saving an image called img1.png you save another file alongside it called img1.txt which contains the data you collect from the EditText(s). How you format this data file will depend a bit on how much data you have to store, and what kind of structure the data needs to represent (if any)
Then when you load up a png to display it you'll need to also load up the txt file that shares the same name, and populate the data contained inside to some TextView or something near your ImageView.
Use a database. Make a table that associates images with the data you want. When you display the picture, read the info for that picture from the database and display that as well. Either store the images directly in the db or store them in a folder and use the filename as the primary key in the database table.
There's number of ways to accomplish this. You could use a hashmap where the String is the data they entered and int is the resourceID of the image. For persistent storage, I recommend an SQLite Database that stores the string in a column and the associated image in another.
A few options:
Save a file with the image.. Give it the same name but .txt or something. The file should probably be stored in the location returned by getFilesDir() . Alternatively you could use SharedPreferences.
Use an sqlite database
Save within the exif data of the image. Use the ExifInterface class with setAttribute/getAttribute to save the values.

how does RSS feed application keep items marked as read? android

I have an application that read an online xml file, now i want to implement such functionality that when the user has clicked on item (read the item), it will marked as read. My problem is how to keep the old items' read status when I request again the new content from that xml file? and when I delete the item from xml file, it then will delete such item on the phone too when the user reload the content.
There's two ways to implement it:
Keep local database (with corresponding Content Provider) which would store read statuses. E.g. You could have table with two fields 'Record Id in online xml' - 'Read status'. Using this way, 'read' status will bee stored locally and on another client user would see items not marked as read;
Send server acknowledgement that the item has been read, so next request it won't be presented in the xml file (or xml file would provide it as 'read'). This way depends on the server implementation, but more convenient because 'read' status might be propagated along many clients;

Create list in android app

I want to know a good way to create a list in my android app. I have all info in my DB and want to load data from it each time I start the app and make a list from it (id and title).
What is the best approach?
Should I make a PHP-script that responds with a JSON encoded array with all list items or should I make an XML-file that generates each time the data in the DB changes that I import to the app each time it starts? or any other good way to do it?
Since all stuff are made by XML-files in android it feels like importing a XML would be a good thing, is it? And how do I import an XML-file from a web server into the app?
// Daniel
You can use either JSON or XML.
You can use the web service approach or you can include your db with your application.
In fact, I most often choose to create a sqlite3 database of my data and include it in the assets folder, which can be copied to the app's data folder on startup.
As for copying your sqlite3 database from assets/ to the db data directory, I found these instructions helpful.
In your situation I would pick JSON over XML for all the reason's stated in the following post: http://ajaxian.com/archives/json-vs-xml-the-debate
Plus, in android, there are JSON Array's built in by default so you don't have to do any extra passing of the code.
return new JSONArray("my json string goes here...");
Since we are talking about a mobile device, I would always generate changes in your php script rather than have a full sync as this will be a lot smaller in size that a full sync. However, you will need to give your user a option to do a full re-sync if this is applicable to your app. I would use a SQLite database to store the data and only update the changes in that.
To also make the stream smaller, you can gzip compress your output from php as this can be natively read by the android device. In my app, I compress 500kb down to ~110kb before transmitting, a huge saving on performance. Here a partial example of how to read the stream:
InputStream in = null;
HttpURLConnection httpConn = null; // you will have to write your on code for this bit.
if (httpConn.getContentEncoding() != null)
{
String contentEncoding = httpConn.getContentEncoding().toString();
if (contentEncoding.contains("gzip"))
{
in = new GZIPInputStream(httpConn.getInputStream());
}
}
else
{
in = httpConn.getInputStream();
}
I hope that this all makes sense, it's been a long day programming :)
Stu

Categories

Resources