Ad

Monday, November 18, 2013

Android Camera Intent

private static int TAKE_PICTURE = 1;    
private Uri imageUri;

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }
}

Android Camera Intent

private static int TAKE_PICTURE = 1;    
private Uri imageUri;

public void takePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }
        }
    }
}
 
source : http://stackoverflow.com/questions/2729267/android-camera-intent 

Sunday, November 10, 2013

Monitoring Foreground Activity in Android

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 
Log.d("topActivity", "CURRENT Activity ::"
            + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
 
You will need the following permission:

<uses-permission android:name="android.permission.GET_TASKS"/>
 
 
source : http://stackoverflow.com/questions/17629053/how-do-android-monitor-usage-applications-work 

Wednesday, October 30, 2013

How to Solve OutOfMemory Error (OOM) in Android ?

    How to Solve OutOfMemory Error (OOM) in Android ?


When you try to decode large files on your Android device (where you have only 16MB memory available) you wil surely get an OutOfMemory exception.

We have found that if you create a temp storage and pass it to BitmapFactory before decoding might help.

So, before using BitmapFactory.decodeFile() create a byte array of 16kb and pass it to temp storage in decoding process.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);

Also you might consider passing your own outputstream that streams out to server or other storage to minimize memory consumtion during the process.

PS, do not forget to bitmapImage.recycle() after usage.

Also consider using options.inSampleSize with values other than 1. From SDK: If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. (1 -> decodes full size; 2 -> decodes 1/4th size; 4 -> decode 1/16th size). Because you rarely need to show and have full size bitmap images on your phone. For manipulations smaller sizes are usually enough.


Source: http://eclipseandroid.blogspot.in/2012/02/how-to-solve-outofmemory-error-oom-in.html

Friday, September 27, 2013

Android code to check internet availability

ConnectivityManager cm = 
(ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

if(cm.getActiveNetworkInfo()!=null && cm.getActiveNetworkInfo().isConnected() &&    cm.getActiveNetworkInfo().isAvailable())
{
return true;
}
else
{
return false;
}

Monday, September 23, 2013

Hide Keyboard in Android

Method 1:

getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


Method 2:


InputMethodManager imm = (InputMethodManager)getSystemService( Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Monday, July 29, 2013

How to install an Android Driver in the Window PC?

Step 1:
   Download the drivers from the website..
   For example: You can get asus drivers from here.

Step 2:
  Connect the device to the system.

Step 3:
   Open the device Manager.
      Win7:help
      WinXP:help

Step 4:
  Select the device(in the Other device).

Step 5:
   Select the button "Reinstall Driver".

   Then choose the driver file.  And install..

How to enable DEVELOPER OPTION in NEXUS 7 (Android 4.2.2)

Step 1:
 Open the settings.

Step 2:
  Tap the "About Tablet"/ "About Phone" in the system..

Step 3:
  Repeatedly tap on Build Number (5+ taps) until you got a message:"you are now a developer!"
  

Sunday, July 7, 2013

How to copy sqlite db from Asset Folder in Android ?

 1. Put the dbfile in the asset folder.

2. Create an instance to the dbFile.

File dbfile=new File(parentfolder,dbfile); (we can choose any folder as parent folder)

3. Check whether db is existing or not. if not copy it to the folder.

if (!dbfile.exists()) {
try {
copyDatabase(dbfile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}

private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);
 
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
 
os.flush();
os.close();
is.close();
}

Friday, June 28, 2013

Android SQLite Commands with Group By and HAVING

 SELECT * from survey_answer_list_main





SELECT question_id,answer, count(answer)  from survey_answer_list_main where survey_id=1 AND type='single'  group by question_id,answer;

SELECT question_id,answer, count(answer)  from survey_answer_list_main where survey_id=1 AND type='single'  group by question_id,answer having answer LIKE '%yes%'



Friday, June 14, 2013

Multi-Selection ListView ANDROID with CheckBox

Multi selection

http://dj-android.blogspot.in/2012/04/milti-selection-listview-android-with.html

To get the selected rows

del_selected.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SavelistAdapter sladapt=(SavelistAdapter) list_save.getAdapter();
String inarray="(";
for(int i=0;i<sladapt.getCount();i++){
mItem=sladapt.getItem(i);
if(nm.isChecked()){
inarray+=""+nm.getId()+",";
}

}
inarray=inarray.substring(0, inarray.length()-1)+")";
Log.i("zacharia", "i ="+inarray);
}

});

Monday, June 10, 2013

Thursday, May 30, 2013

How to move icons between different screens in Application Menu in Android ?

1. Open the Application Menu Screen.

2. Press the menu button and select "Edit".

3. Press the icon to move and drag it to the right/ left edge and hold for a second.

4. Then place it where you want.

Sunday, May 26, 2013

Android Studio not starting

After installing Android studio, it wouldn't run (normal or admin didn't change a thing). 
You will need to create a system environment variable named JDK_HOME with your JDK installation path (in my case C:\Program Files\Java\jdk1.7.0_21\ ). Android studion should now launch.