Pages

Thursday, 7 January 2016

getView called multiple times

First check this question in detail : http://stackoverflow.com/questions/11186004/yet-another-getview-called-multiple-times

 short explanation about listview.

Thumb rule : never use wrap_content for listview's height and width.

if we use height or width of listview as wrap_content then adapter's getview will call more then once for same item after notifydatasetchanged.

to overcome this situation as suggested in above link use listview's height and width as match_parent or fixed size.

Tuesday, 22 December 2015

Android Mime Type File manager

In Following link have working example of Mime Type File manager.

It will display photo,video or audio from external attached device. in case if it's not working than remove sd-card and re-insert it. then try. or try with phone restart.

Demo :
https://sites.google.com/site/fancifulandroid/android-projects/USB-connection%28FileManager%29.zip?attredirects=0&d=1

Android get mime type of file or url

Use following function to get MIME Type of file. pass file path or any url

public static String getMimeType(String url) {
  try {
   String type = null;
   String extension = url.substring(url.lastIndexOf(".") + 1, url.length());
   Log.i("extension", "ext : " + extension);
   if (extension != null) {
    type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
   }
   return type;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
}

Thursday, 22 October 2015

Android sql prepared statements, how to prevent sql injection

Generally do not use query directly as string.
Android have default classes so we can use those function to prevent sql injection.

For ex,

To get records from db and suppose you have joins or sub query so default 'query' may no use in such case we will use following funcitons.

String sql = "select *,(select type from table 2 where table2.type=? limit 1) as type from table1 where table1.name like ?";

DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();

Cursor c = sqLiteDatabase.rawQuery(sql, new String[]{"car","m%"});


If we have "IN" clause in sql statement then we may use it following way.

String[] names = { "name1", "name2" }; // do whatever is needed first
String query = "SELECT * FROM table"
    + " WHERE name IN (" + TextUtils.join(",", Collections.nCopies(names.length, "?"))  + ")";
Cursor cursor = mDb.rawQuery(query, names); 


For insert,update and delete we may use default following functions

insert (String table, String nullColumnHack, ContentValues values) ;

update (String table, ContentValues values, String whereClause, String[] whereArgs); 

delete(String table, String whereClause, String[] whereArgs)

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) // we may use it for simple SELECT query

Monday, 12 October 2015

Android play video programmatically

Use following code to play video.

vvVideo = (VideoView) findViewById(R.id.vvVideo);
// use this to play video from raw resource
  vvVideo.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.main));
// use this to play video from url (streaming)
  vvVideo.setVideoURI(Uri.parse("http://..."));
// if possible do not use height to match_parent. It may create problem. I am not sure about this.
// play video from asset
  vvVideo.setVideoURI(Uri.parse("file:///android_asset/path/to/your.mp4"));
// play video from sdcard
  vvVideo.setVideoURI(Uri.fromFile(new File("/sdcard/cats.jpg")));


  vvVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
    // video play complete
   }
  });

  vvVideo.start();

use following xml

<VideoView
        android:id="@+id/vvVideo"
        android:layout_width="match_parent"
        android:layout_height="488dp"
        android:layout_centerInParent="true"
        />

add read external storage permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>