Friday, December 16, 2011

Saving files to External Storage in Android


Just a snippet of code for android that can help some of my friends.
String FILENAME = "myfile.txt";
boolean externalStorageMounted = false;
boolean externalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
 externalStorageMounted = true;
 externalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
 externalStorageMounted = true;
 externalStorageWriteable = false;
} else {
 externalStorageMounted = false;
 externalStorageWriteable = false;
}

if (externalStorageMounted && externalStorageWriteable) {
 File dbfile = new File(Environment.getExternalStorageDirectory(), FILENAME);
 FileOutputStream fos = new FileOutputStream(dbfile, true);
 //Do anything with fos
 try {
  if (fos != null)
   fos.close();
 } catch (IOException e) {
  e.printStackTrace();
 }
}
Ok, almost done, but not yet.Open your AndroidManifest.xml and add the following line

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

or  using graphical interface: Permissions -> Add... -> android.permission.WRITE_EXTERNAL_STORAGE

And everything ready.

Wednesday, December 14, 2011

Recently a friend of mine told me about an attempt to put Android 4.0 on Samsung GT540, it seemed too unrealistic but it is perfectly true, here is the link to the blog of the person who is attempting such a feat.

http://aosp.mikegapinski.cba.pl/?page_id=14

And to give a special touch, a video too.

If someone wants to try, do not forget to leave some feedback about this experience :)

Tuesday, December 13, 2011

Downloading files in Java via HTTP

Today a friend of mine asked me a small piece of code for an Android application that allows downloading a file via HTTP from a remote server and save it to a local file. I looked in my documents and quickly found a small download manager that did precisely that.
But now I will share with you a class in Java (android or not) that takes as argument a URL and the destination file and download it. Remember that this code is very basic and is only to give ideas.


public class DownloaderClass {
    public void downloadFile(URL url, File outputFile) {
        int BUFFER_SIZE = 4096;
       
        InputStream in = null;
        FileOutputStream fos = null;
       
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();

            if (conn.getResponseCode() / 100 != 2) {
                throw new RuntimeException("Response Code: " + conn.getResponseCode());
            }
           
            in = conn.getInputStream();
            fos = new FileOutputStream(outputFile);

            byte data[] = new byte[BUFFER_SIZE];
            int numRead;
            while (((numRead = in.read(data)) != -1)) {
                fos.write(data, 0, numRead);
            }
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}



To use it, you just need 2 lines of code in your main.
DownloaderClass dc = new DownloaderClass();
dc.downloadFile(new URL("http://hackedprojects.blogspot.com/favicon.ico"),new File("c:\\favicon.ico"));



As you can see, it's simples :P

Man dies electrocuted while stealing copper cables


Generally I don’t care about daily news, but today I’ll make an exception.
In the last days I've heard news about stolen goods, from diesel from cars to money from the kindergarten‘s Christmas tree, all of them has been a success for the thieves, except that one:




Read text file line-by-line in Java

I know that it's simple, but I'll leave it here.

DataInputStream dis = null;
try {
   dis = new DataInputStream(new FileInputStream("file.txt"));
   BufferedReader br = new BufferedReader(new InputStreamReader(dis));
   String line;
   while ((line = br.readLine()) != null) {
      System.out.println(line);
   }
} catch (Exception e) {
   System.err.println(e.getMessage());
}finally{
   try {
      dis.close();
   } catch (IOException ex) {}
}

Data types conversions in java


In the last weeks I've been programming in Java for 2 school works and others 3 more projects, I'm the type of person how prefer thing more low-level, so I can say that I never used it so much in so little time. The good thing about that is that I'm finally learning many things about objects and events, it's just like a new world.
So I have been taking notes and I'll share a little about a thing that I always forget, how to convert between data types commonly used.


Monday, December 12, 2011

Hello World!


Hi folks, I'm David Silva aka Longinus, a Portuguese computer science student and this is my new blog where I will publish some of my projects, notes, news, reviews and lot more about programming in C /C++, Java, Android and PHP, Linux, Robotics and Electronics. Wish you like and don't forget, leave some comments :P