Tuesday, October 29, 2013

Upload Files to server from Sdcard in android

 Need to Add  Httpmime.jar to your Build path
 Call AsyncTask:
 new UploadFileService ("upload Url",
            file name, file path).execute();

AsyncTask:

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

public class UploadFileService extends AsyncTask<Void, Void, Void> {
    private String posturl;
    private String fileName;
    private String filePath;

    public UploadFileService (String posturl, String filename,
            String filePath) {
        this.fileName = filename;
        this.posturl = posturl;
        this.filePath = filePath;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // new HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(posturl);

        File file = new File(filePath + "/" + fileName);

        FileBody fileBody = new FileBody(file);

        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("file", fileBody);
        httpPost.setEntity(reqEntity);

        // execute HTTP post request
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            try {
                String responseStr = EntityUtils.toString(resEntity).trim();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // you can add an if statement here and do other actions based
            // on response
        }

        return null;
    }

}

List FileNames from Directory in Android


 String path = Environment.getExternalStorageDirectory().toString() + "/directory/dir";
  File f = new File(path);
    if (f.isDirectory()) {
      return;
    } else {
      File file[] = f.listFiles();
     
      Log.d("Files", "Size: " + file.length);
      for (int i = 0; i < file.length; i++) {
Log.d(tag, file[i].getName());
         }
}

Friday, October 18, 2013

How to get list of table names in sqlite database (Android)

Cursor c = db.rawQuery("SELECT name FROM sqlite_master", null);
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
//table gives you table names
String table= c.getString(0);

c.moveToNext();
}
}

Monday, October 14, 2013

Export android Sqlite database to CSV

you need to add opencsv lib to your project :Open csv Download link
add jar to your app library
Add Permission:
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
CODE:
File dbFile = getDatabasePath("YourDatabase.db");

        DataBaseHelper DBob = new DataBaseHelper(MainActivity.this);


        File exportDir = new File(Environment.getExternalStorageDirectory()

                .getPath(), "");

        if (!exportDir.exists())


        {


            exportDir.mkdirs();


        }


        File file = new File(exportDir, "Filename.csv");


        try


        {


            file.createNewFile();


            CSVWriter csvWrite = new CSVWriter(new FileWriter(file));


            SQLiteDatabase db = DBob.getReadableDatabase();


            Cursor curCSV = db.rawQuery("SELECT * FROM Table_Name", null);


          //  int c = curCSV.getColumnCount();


            csvWrite.writeNext(curCSV.getColumnNames());


            while (curCSV.moveToNext())  {


                String arrStr[] = { curCSV.getString(0), curCSV.getString(1),


                curCSV.getString(2) };


                csvWrite.writeNext(arrStr);


            }

 csvWrite.close();
 curCSV.close();
 }
  catch (SQLException sqlEx)   {
 Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
  }
 catch (IOException e) {
 Log.e("MainActivity", e.getMessage(), e);
 }