Friday, October 21, 2011

Android Save Image to Media Provider


public Uri saveMediaEntry(String imagePath,String title,String description,long dateTaken)
{
ContentValues image = new ContentValues();

image.put(Images.Media.TITLE, "ImageTitle");
image.put(Images.Media.DISPLAY_NAME, "Heart");
image.put(Images.Media.DESCRIPTION, "description");
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/jpeg");
image.put(Images.Media.ORIENTATION, 0);
File imageFile = new File(imagePath) ;
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put("_data", imageFile.getAbsolutePath());
return getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
}



Android Save Image to Media Provider


public Uri saveMediaEntry(String imagePath,String title,String description,long dateTaken)
{
ContentValues image = new ContentValues();

image.put(Images.Media.TITLE, "ImageTitle");
image.put(Images.Media.DISPLAY_NAME, "Heart");
image.put(Images.Media.DESCRIPTION, "description");
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/jpeg");
image.put(Images.Media.ORIENTATION, 0);
File imageFile = new File(imagePath) ;
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put("_data", imageFile.getAbsolutePath());
return getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
}



Thursday, October 20, 2011

Bitmap out of memory

Image size Out of Memory you can use this code:
(android BitmapFactory.decodeStream out of memory error)

BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(files[i].getAbsolutePath());
BitmapFactory.decodeStream(fis, null, o);
fis.close();
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = scale;
fis = new FileInputStream(files[i].getAbsolutePath());
bitmap = BitmapFactory.decodeStream(fis, null, op);
fis.close();
===============================
you can also use this :
bitmap.recycle();
after using the bitmap every time use this method so that it will free the memory and now memory will be available for next bitmap.
Example:
Bitmap bm = BitmapFactory.decodeStream(instream);

Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);


I am sure it will solve the out of memory problem.



Tuesday, October 18, 2011

Android Compress Image File


f = new FileInputStream("path");

scale=2;

BitmapFactory.Options op=new BitmapFactory.Options();

op.inSampleSize=scale;

Bitmap bitmap=BitmapFactory.decodeStream(f,null,op);



Create Thumbnails of Image

Create Thumbnails of image/Compress Imgage .

This program for to display Thumbnails of images store in sdcard or any other location.

=========================================================

BitmapFactory.Options op=new BitmapFactory.Options();

InputStream f = null;

try {

f = new FileInputStream(files[position].getAbsolutePath());

} catch (FileNotFoundException e) {

e.printStackTrace();

}

final int REQUIRED_SIZE=1024;

int width_tmp=op.outWidth, height_tmp=op.outHeight;

int scale=1;

while(true){

if(width_tmp/2

break;

width_tmp/=2;

height_tmp/=2;

scale*=2;

}

op.inSampleSize=scale;

Bitmap b2; b2=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(files[position].getAbsolutePath(),op),50, 50,true);

imageView.setImageBitmap(b2);

=========================================================

Here while loop is used because image file can me more than 1 mb in size so there may be cresh or it can take more time to set in imgageview, so we reducing scale of that image.



Saturday, October 15, 2011

Android Introduction and Architecture(Part1)




What is Android?

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

Features

  • Application framework enabling reuse and replacement of components
  • Dalvik virtual machine optimized for mobile devices
  • Integrated browser based on the open source WebKit engine
  • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
  • SQLite for structured data storage
  • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
  • GSM Telephony (hardware dependent)
  • Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
  • Camera, GPS, compass, and accelerometer (hardware dependent)
  • Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Ec

Android Architecture

The following diagram shows the major components of the Android operating system. Each section is described in more detail below.


Applications

Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language.

Application Framework

By providing an open development platform, Android offers developers the ability to build extremely rich and innovative applications. Developers are free to take advantage of the device hardware, access location information, run background services, set alarms, add notifications to the status bar, and much, much more.
Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components; any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user.
Underlying all applications is a set of services and systems, including:
  • A rich and extensible set of Views that can be used to build an application, including lists, grids, text boxes, buttons, and even an embeddable web browser
  • Content Providers that enable applications to access data from other applications (such as Contacts), or to share their own data
  • A Resource Manager, providing access to non-code resources such as localized strings, graphics, and layout files
  • A Notification Manager that enables all applications to display custom alerts in the status bar
  • An Activity Manager that manages the lifecycle of applications and provides a common navigation backstack
For more details and a walkthrough of an application, see the Notepad Tutorial.

Libraries

Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Some of the core libraries are listed below:
  • System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices
  • Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG
  • Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications
  • LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view
  • SGL - the underlying 2D graphics engine
  • 3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer
  • FreeType - bitmap and vector font rendering
  • SQLite - a powerful and lightweight relational database engine available to all applications

Android Runtime

Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language.
Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has been written so that a device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool.
The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management.

Linux Kernel

Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack.

install Eclips and Androd Sdk on your system and do start Android Programming. Click on: Start


Friday, October 14, 2011

Android Check the Availability of Network Example

CHECK THE AVAILABILTY OF INTERNET

SOURCE CODE [main.xml] is


<?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" />
<Button android:text="Check Network Status"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

SOURCE CODE
[NetworkCheck.java] is

package com.NetworkCheck;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class NetworkCheck extends Activity
{

Button check;

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

check = (Button) findViewById(R.id.button1);

check.setOnClickListener(new OnClickListener()
{

public void onClick(View v)
{

if(isInternetOn())
{
Toast.makeText(getBaseContext(), "Connected",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Not connected",
Toast.LENGTH_SHORT).show();
}

}});

}

public final boolean isInternetOn()
{

ConnectivityManager connec = (ConnectivityManager) getSystemService
(Context.CONNECTIVITY_SERVICE);

if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
{
return true;
}

else if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
|| (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
{
return false;
}

return false;
}

}

Note:
Don’t forget to add   
in your AndroidManifest.xml.


<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>


The OUTPUT will be



Android - Search in Custom ListView Example

SEARCH IN CUSTOM LISTVIEW
SOURCE CODE [main.xml] is

<?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">

<EditText android:id="@+id/EditText01"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:hint="Search">

</EditText>

<ListView android:id="@+id/ListView01"

android:layout_height="wrap_content"

android:layout_width="fill_parent">

</ListView>


</LinearLayout>

SOURCE CODE [listview.xml] is

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"

android:gravity="left|center"

android:layout_width="fill_parent"

android:paddingBottom="5px"

android:background="#fff200"
android:paddingTop="5px"

android:paddingLeft="5px">


<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"

android:layout_height="wrap_content">

</ImageView>

<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20px"
android:textStyle="bold"
android:layout_marginLeft="10px"

android:textColor="#0099CC">

</TextView>

</LinearLayout>

SOURCE CODE [CustomListViewSearch.java] is

package com.CustomListViewSearch;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListViewSearch extends Activity
{
EditText edittext;
ListView listview;

String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten" };

int[] image = { R.drawable.one, R.drawable.two, R.drawable.three,
R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
R.drawable.eight, R.drawable.nine, R.drawable.ten };
int textlength = 0;
ArrayList text_sort = new ArrayList();
ArrayList image_sort = new ArrayList();

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

edittext = (EditText) findViewById(R.id.EditText01);
listview = (ListView) findViewById(R.id.ListView01);
listview.setAdapter(new MyCustomAdapter(text, image));
edittext.addTextChangedListener(new TextWatcher()
{

public void afterTextChanged(Editable s)
{

}

public void beforeTextChanged(CharSequence s, int start,
int count, int after)
{

}

public void onTextChanged(CharSequence s, int start,
int before, int count)
{

textlength = edittext.getText().length();
text_sort.clear();
image_sort.clear();

for(int i=0; i<text.length; i++)
{
if (textlength <= text[i].length())
{
if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
text_sort.add(text[i]);
image_sort.add(image[i]);
}
}
}

listview.setAdapter(new MyCustomAdapter
(text_sort, image_sort));

}
});
}

class MyCustomAdapter extends BaseAdapter
{

String[] data_text;
int[] data_image;

MyCustomAdapter()
{

}

MyCustomAdapter(String[] text, int[] image)
{
data_text = text;
data_image = image;
}
MyCustomAdapter(ArrayList text, ArrayList image)
{
data_text = new String[text.size()];
data_image = new int[image.size()];

for(int i=0; i<text.size(); i++)
{
data_text[i] = text.get(i);
data_image[i] = image.get(i);
}

}

public int getCount()
{
return data_text.length;
}

public String getItem(int position)
{
return null;
}

public long getItemId(int position)
{
return position;
}

public View getView(int position, View convertView, ViewGroup parent)
{

LayoutInflater inflater = getLayoutInflater();
View row;

row = inflater.inflate(R.layout.listview, parent, false);

TextView textview = (TextView) row.findViewById(R.id.TextView01);
ImageView imageview = (ImageView) row
.findViewById(R.id.ImageView01);

textview.setText(data_text[position]);
imageview.setImageResource(data_image[position]);

return (row);

}
}

}

The OUTPUT will be