Oct 12, 2011

Get File Extension and MIME Type

We can call the methods getFileExtensionFromUrl() and getMimeTypeFromExtension() of android.webkit.MimeTypeMap class to get File Extension and MIME Type of a file.

example:

Get File Extension and MIME Type

package com.AndroidListFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListFilesActivity extends ListActivity {

private List<String> fileList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

File root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
ListDir(root);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
}else {
Uri selectedUri = Uri.fromFile(selected);
String fileExtension
= MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
String mimeType
= MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

Toast.makeText(AndroidListFilesActivity.this,
"FileExtension: " + fileExtension + "\n" +
"MimeType: " + mimeType,
Toast.LENGTH_LONG).show();

}
}

void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}

ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
}


Related Posts:
- List files/directory in Android
- Navigation in files/directory
- Start activity using intent of ACTION_VIEW, for specified MIME type.



Oct 11, 2011

Navigation in files/directory

Navigation in files/directory

package com.AndroidListFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListFilesActivity extends ListActivity {

private List<String> fileList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

File root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
ListDir(root);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File selected = new File(fileList.get(position));
if(selected.isDirectory()){
ListDir(selected);
}else {
Toast.makeText(AndroidListFilesActivity.this,
selected.toString() + " selected",
Toast.LENGTH_LONG).show();
}
}

void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}

ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}

}


prev:
- List files/directory in Android

next:
- Get File Extension and MIME Type



Oct 10, 2011

List files/directory in Android

List files/directory in Android
package com.AndroidListFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;

public class AndroidListFilesActivity extends ListActivity {

private List<String> fileList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

File root = new File(Environment
.getExternalStorageDirectory()
.getAbsolutePath());
ListDir(root);

}

void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getPath());
}

ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}

}



next:
- Navigation in files/directory



Oct 8, 2011

Set background of ListView using drawable

To set background of istView to drawable, simple add "android:background" property in ListView to specify the background. And add "android:cacheColorHint" propertity also, otherwise, the background will become abnormal while scrolling.

exampe:
<ListView  
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:cacheColorHint="#00000000" />


Set background of ListView using drawable


Oct 3, 2011

Embeded Google Chart in WebView

Google Chart Tools provide a funny Chart Wizard to generate image charts. By using WebView, it's easy to embedded the charts inside your App.

Embeded Google Chart in WebView

In the Chart Wizard, select your chart, enter data, lets the wizard generate the chart, copy the link of "Paste link in email or IM" to embeddedWeb in the code.

Noted: In order to load the generated charts, permission of "android.permission.INTERNET" is need.

package com.AndroidEmbeddedWebView;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class AndroidEmbeddedWebViewActivity extends Activity {

WebView embeddedWebView;
String embeddedWeb = "http://chart.apis.google.com/chart?chxl=0:|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec&chxt=x,y&chs=300x300&cht=r&chco=FF0000&chd=t:63,64,67,73,77,81,85,86,85,81,74,67,63&chls=2,4,0&chm=B,FF000080,0,0,0";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
embeddedWebView = (WebView)findViewById(R.id.embeddedwebview);
embeddedWebView.loadUrl(embeddedWeb);
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<WebView
android:id="@+id/embeddedwebview"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
/>
</LinearLayout>

Infolinks In Text Ads