StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display total External Storage Memory
Friday, September 16, 2011
Calculate Available External Storage memory
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();;
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display available External Storage Meomry
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();;
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display available External Storage Meomry
Calculate Total Internal Storage memory
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display total Internal Storage Memory
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display total Internal Storage Memory
Calculate Available Internal Storage memory
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();;
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display available Internal Storage Memory
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();;
long megAvailable = bytesAvailable / 1048576;
Here megAvailable display available Internal Storage Memory
Load Image From Url
Below function load image from web url and return drawable.
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
Tuesday, November 9, 2010
Check Internet Connection
import YourPackageName
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.widget.Toast;
class CheckInternetConnection
{
public Context context = null;
public ProgressDialog dialogProcess = null;
public CheckInternetConnection(Context ctx)
{
this.context = ctx;
}
@SuppressWarnings("unchecked")
public boolean CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected())
{
return true;
}
else if (!mobile.isConnected())
{
Toast.makeText(context,"No Internet Connection",5000).show();
return false;
}
else
{
new AsyncCheckInternet().execute();
}
return false;
}
private class AsyncCheckInternet extends AsyncTask
{
@Override
protected void onPreExecute()
{
dialogProcess = ProgressDialog.show(context, "","Checking Internet Connection...",true);
}
@Override
protected Boolean doInBackground(Object... arg0)
{
try
{
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000); // This is time limit if the connection time limit
urlc.connect();
if (urlc.getResponseCode() == 200)
{
return true;
}
}
catch (MalformedURLException e1)
{
e1.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Object result)
{
super.onPostExecute(result);
dialogProcess.dismiss();
}
}
}
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.widget.Toast;
class CheckInternetConnection
{
public Context context = null;
public ProgressDialog dialogProcess = null;
public CheckInternetConnection(Context ctx)
{
this.context = ctx;
}
@SuppressWarnings("unchecked")
public boolean CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected())
{
return true;
}
else if (!mobile.isConnected())
{
Toast.makeText(context,"No Internet Connection",5000).show();
return false;
}
else
{
new AsyncCheckInternet().execute();
}
return false;
}
private class AsyncCheckInternet extends AsyncTask
{
@Override
protected void onPreExecute()
{
dialogProcess = ProgressDialog.show(context, "","Checking Internet Connection...",true);
}
@Override
protected Boolean doInBackground(Object... arg0)
{
try
{
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(3000); // This is time limit if the connection time limit
urlc.connect();
if (urlc.getResponseCode() == 200)
{
return true;
}
}
catch (MalformedURLException e1)
{
e1.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Object result)
{
super.onPostExecute(result);
dialogProcess.dismiss();
}
}
}
Sunday, September 19, 2010
Custom Listview with Image
public static ArrayList vidID = new ArrayList();
public static Hashtable> VideoTable = new Hashtable>();
Hashtable _tempVideo = new Hashtable();
ArrayList db_id;
--Start any required Loop
_tempVideo.put("id", curValue);
_tempVideo.put("title", curValue);
_tempVideo.put("filename", curValue);
_tempVideo.put("thumbnail", curValue);
VideoTable.put(curVidId, _tempVideo);
vidID.add(curVidId);
_tempVideo = new Hashtable();
---End Loop
private void fillData() {
ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList> mylist = new ArrayList>();
HashMap map;
db_id = new ArrayList();
for (int i = 0; i < vidID.size(); i++) {
Hashtable temp = VideoTable
.get(vidID.get(i));
try {
map = new HashMap();
map.put("id", temp.get("id"));
map.put("title", temp.get("title"));
map.put("description", temp.get("description"));
map.put("theartist", temp.get("theartist"));
map.put("duration", temp.get("duration"));
map.put("views", temp.get("views") + " views");
map.put("server", temp.get("server"));
map.put("filename", temp.get("filename"));
map.put("thumbnail", temp.get("thumbnail"));
mylist.add(map);
db_id.add(vidID.get(i));
} catch (Exception e) {
}
}
SimpleAdapter videolist = new SimpleAdapter(this, mylist,
R.layout.videorow, new String[] { "id", "title", "views",
"duration", "description", "server", "filename",
"thumbnail"}, new int[] { R.id.TextView05,
R.id.TextView01, R.id.TextView02, R.id.TextView03,
R.id.TextView04, R.id.TextView06, R.id.TextView07,
R.id.TextView08 });
list.setAdapter(videolist);
list.setOnItemClickListener(this);
list.setTextFilterEnabled(true);
list.setOnHierarchyChangeListener(this);
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
TextView filename = (TextView) arg1.findViewById(R.id.TextView07);
String url = filename.getText().toString();
Log.e(".", url);
Bundle bundle = new Bundle();
bundle.putString("URL", url);
Intent myIntent = new Intent();
myIntent.setClassName("Package Name", "Package Name With Class");
myIntent.putExtras(bundle);
startActivity(myIntent);
}
@Override
public void onChildViewAdded(View parent, View child) {
View row = child;
try {
ImageView imView = (ImageView) row.findViewById(R.id.ImageView01);
imView.setImageBitmap(null);
TextView id = (TextView) row.findViewById(R.id.TextView05);
TextView thumbnail = (TextView) row.findViewById(R.id.TextView08);
TextView filename = (TextView) row.findViewById(R.id.TextView07);
imView.setTag(filename.getText().toString());
String imgurl = thumbnail.getText().toString();
grabURL(imgurl, imView, filename.getText().toString());
ImageView addFav = (ImageView) row.findViewById(R.id.ImageView02);
addFav.setTag(id.getText().toString());
addFav.setOnClickListener(myListner);
} catch (Exception e) {
}
}
@Override
public void onChildViewRemoved(View parent, View child) {
try {
View row = child;
ImageView imView = (ImageView) row.findViewById(R.id.ImageView02);
imView.setTag(null);
imView.setImageBitmap(null);
} catch (Exception e) {
}
}
public void grabURL(String url, ImageView iv, String name) {
GrabURL a = new GrabURL();
a.imgView1 = iv;
a.name = name;
a.execute(url);
}
private class GrabURL extends AsyncTask {
ImageView imgView1;
String name;
protected void onPreExecute() {
}
protected Bitmap doInBackground(String... urls) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(urls[0]);
} catch (MalformedURLException e) {
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (Exception e) {
Log.e(".", e.getMessage());
}
return bmImg;
}
protected void onPostExecute(Bitmap bm) {
if (name == imgView1.getTag().toString())
imgView1.setImageBitmap(bm);
}
}
public static Hashtable> VideoTable = new Hashtable>();
Hashtable _tempVideo = new Hashtable();
ArrayList db_id;
--Start any required Loop
_tempVideo.put("id", curValue);
_tempVideo.put("title", curValue);
_tempVideo.put("filename", curValue);
_tempVideo.put("thumbnail", curValue);
VideoTable.put(curVidId, _tempVideo);
vidID.add(curVidId);
_tempVideo = new Hashtable();
---End Loop
private void fillData() {
ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList> mylist = new ArrayList>();
HashMap map;
db_id = new ArrayList();
for (int i = 0; i < vidID.size(); i++) {
Hashtable temp = VideoTable
.get(vidID.get(i));
try {
map = new HashMap();
map.put("id", temp.get("id"));
map.put("title", temp.get("title"));
map.put("description", temp.get("description"));
map.put("theartist", temp.get("theartist"));
map.put("duration", temp.get("duration"));
map.put("views", temp.get("views") + " views");
map.put("server", temp.get("server"));
map.put("filename", temp.get("filename"));
map.put("thumbnail", temp.get("thumbnail"));
mylist.add(map);
db_id.add(vidID.get(i));
} catch (Exception e) {
}
}
SimpleAdapter videolist = new SimpleAdapter(this, mylist,
R.layout.videorow, new String[] { "id", "title", "views",
"duration", "description", "server", "filename",
"thumbnail"}, new int[] { R.id.TextView05,
R.id.TextView01, R.id.TextView02, R.id.TextView03,
R.id.TextView04, R.id.TextView06, R.id.TextView07,
R.id.TextView08 });
list.setAdapter(videolist);
list.setOnItemClickListener(this);
list.setTextFilterEnabled(true);
list.setOnHierarchyChangeListener(this);
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
TextView filename = (TextView) arg1.findViewById(R.id.TextView07);
String url = filename.getText().toString();
Log.e(".", url);
Bundle bundle = new Bundle();
bundle.putString("URL", url);
Intent myIntent = new Intent();
myIntent.setClassName("Package Name", "Package Name With Class");
myIntent.putExtras(bundle);
startActivity(myIntent);
}
@Override
public void onChildViewAdded(View parent, View child) {
View row = child;
try {
ImageView imView = (ImageView) row.findViewById(R.id.ImageView01);
imView.setImageBitmap(null);
TextView id = (TextView) row.findViewById(R.id.TextView05);
TextView thumbnail = (TextView) row.findViewById(R.id.TextView08);
TextView filename = (TextView) row.findViewById(R.id.TextView07);
imView.setTag(filename.getText().toString());
String imgurl = thumbnail.getText().toString();
grabURL(imgurl, imView, filename.getText().toString());
ImageView addFav = (ImageView) row.findViewById(R.id.ImageView02);
addFav.setTag(id.getText().toString());
addFav.setOnClickListener(myListner);
} catch (Exception e) {
}
}
@Override
public void onChildViewRemoved(View parent, View child) {
try {
View row = child;
ImageView imView = (ImageView) row.findViewById(R.id.ImageView02);
imView.setTag(null);
imView.setImageBitmap(null);
} catch (Exception e) {
}
}
public void grabURL(String url, ImageView iv, String name) {
GrabURL a = new GrabURL();
a.imgView1 = iv;
a.name = name;
a.execute(url);
}
private class GrabURL extends AsyncTask {
ImageView imgView1;
String name;
protected void onPreExecute() {
}
protected Bitmap doInBackground(String... urls) {
URL myFileUrl = null;
Bitmap bmImg = null;
try {
myFileUrl = new URL(urls[0]);
} catch (MalformedURLException e) {
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (Exception e) {
Log.e(".", e.getMessage());
}
return bmImg;
}
protected void onPostExecute(Bitmap bm) {
if (name == imgView1.getTag().toString())
imgView1.setImageBitmap(bm);
}
}
Subscribe to:
Posts (Atom)