Monday, January 21, 2013

File IO in Android

This article shows how to make an application that can write to external storage of android device. First off all we create a new project in Eclipse, say WriteFile.
Now, we will use the method getExternalStorageState() of Environment class to get detail about the external storage state. It will return a string showing the state of external device.
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String status = Environment.getExternalStorageState();
    }

}
We check if it is anything else than Environment.MEDIA_MOUNTED then we terminate the app with appropriate message else we continue.
 

        String status = Environment.getExternalStorageState();
        if(!status.equals(Environment.MEDIA_MOUNTED))
         return;

Now, we know we have external storage and it also has read/write permissions. Next is to get a File object referring to the external director. Now depending on the device this may vary. So we use getExternalStorageDirectory() method to get the File object and save reference in sdcard variable.
        File sdcard = Environment.getExternalStorageDirectory();

Now we want to create a file, say a dummy.txt in the directory. To do this first obtain a reference to that file using File(String name) constructor. Then we use character streams to write data to file (We can also use the Byte Streams). Create a FileWriter to write data to file and a FileReader to read data from the file. In both constructors pass the File object of our target file. Write() method of FileWriter is used to write data to file.
        char [] buf=null;
        File newfile = new File(sdcard.getAbsolutePath()+"/dummy.txt");
        FileWriter fileWriter = new FileWriter(newfile);
        fileWriter.write("This is a demo");
        FileReader fileReader = new  FileReader(newfile);
        buf = new char[50];
        fileReader.read(buf);
Now the data from file is in buf array. We can use it the way we want now. The Full Source code is available here .

Notes : Creating File object generates FileNotFoundExceptiion and FileReader and FileWriter Both generates IOExcption, so be carefull to surround the statements with try/catch block.

Writing to external storage requires the WRITE_EXTERNAL_STORAGE permission. In a future platform release, access to this path will require the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.



Ads :
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.com | www.ebay.ch | www.ebay.com.my

Friday, September 21, 2012

Moving an element along a path with jquery

Many time you want to move a object (div, img) on specific path. suppose your path is not much complicated but it the combination of line, means many lines together creates the path, then you can use the following method.
For this tutorial suppose you have a div, obj and you want it to move.
First of all you will need a to create path, so as I said earlier your path is made of lines, and line have to ends.
So we will store coordinates in a two dimensional array.

 var SIZE = 4; // No of points
 var path = new Array(SIZE+1); // one extra point to complete cycle
 
 i=0;
 while(i < path.lentgh)
 {
  path[i] = new Array(2);
  i++;
 }
 // Now we have a two dimensional array
 
 // setting path,
 path[0][0] = 0;    path[0][1] = 0;
 path[1][0] = 0;    path[1][1] = 700;
 path[2][0] = 1000; path[2][1] = 700;
 path[3][0] = 0;    path[3][1] = 7;

 //completing cycle
 path[SIZE][0] = path[0][0];
 path[SIZE][1] = path[0][1];


So now we have the path, we are going to use jquery animate function, so we are going to need time also. Now distance between two poits may vary so in stead of setting time, we will set speed and calculate time from it.
 SPEED = 3;
 var itime = new Array(SIZE);
 for(i=0;i < SIZE; i++)
     itime[i] = d(i)*SPEED;
 

Here d(i) is a function defined below that gives the distance between ith ane i+1th point and we are not using speed here its actually inverse of speed. Thus increasing it will slow the animation.

 function d(i)
 {
    // for simplicity
    x1 = path[i][0];
    x2 = path[i+1][0];
    y1 = path[i][1];
    y2 = path[i+1][1];

    return Math.floor(Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ))
 }

So now we have time and points for animation. So next step is to animate it.
 t = 0;
 function animateobj(obj)
 {
    $(obj).animate({left:path[t+1][0], top:path[t+1][1]}, itime[t]);
    setTimeout(function(){animateobj(obj);}, itime[t]);
    t++;
    if(t == SIZE)
       t = 0;
 }


And you are done, now from your file, just call animateobj method with appropriate id and it will do the animation.
NOTE: set path, itime, SIZE, SPEED, and t as global do not create them inside a function and also you will need jquery which is included in demo.
Click here to download demo.

Hey, if you liked it, please click on ad below...


Ads :
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.com | www.ebay.ch | www.ebay.com.my