Monday, 4 February 2013

Send application crash report via email in Android

This is application crash reporting for android. It will give all the unhandled exception detail on developer mail that cause crash application because of memory reason etc.

Developers can use this in their projects to know the unhandled exception detail and as well as device detail.

To download complete project, please click here.

Monday, 24 December 2012

Save Bitmap in Internal and External Memory in Android


/**
* Method to save bitmap into internal memory
* @param image and context
*/
public boolean saveImageToInternalStorage(Bitmap image,Context context)
{
try {
FileOutputStream fos = context.openFileOutput("photo.jpg", Context.MODE_WORLD_READABLE);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                // 100 means no compression, the lower you go, the stronger the compression
fos.close();
return true;
}
catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
return false;
}

/**
 * Method to save bitmap into external storage
* @param image
* @return true if save success otherwise false
*/
public void saveImageToExternalStorage(Bitmap image) {
//image=scaleCenterCrop(image,200,200);
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(fullPath, "photo1.png");

if(file.exists())
file.delete();

file.createNewFile();
fOut = new FileOutputStream(file);
// 100 means no compression, the lower you go, the stronger the compression
image.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
Log.e("saveToExternalStorage()", e.getMessage());
}
}

Check whether device is Tablet Programatically in Android

To check whether device is tablet progrmatically at run time in android.


/**
* To check device is tablet or not.
* @param context
* @return true if device is tablet
*/
public static boolean isTablet(Context context) {
      boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
      boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
   return (xlarge || large);
}


Share Image with text on Twitter , Facebook and Email etc. in Android


1) For attaching image on gmail with text use below code.

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("gmail",filePath.toString());

2) For sharing image on facebook

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("facebook",filePath.toString());

3) For sharing image with text on twitter

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("com.twitter.android",filePath.toString());

/**
* To share photo with text on facebook , twitter and email etc.@param nameApp
* @param nameApp
* @param imagePath
* /

void share(String nameApp, String imagePath) {
try
{
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
            targetedShare.setType("image/jpeg"); // put here your mime type
            if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
             targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by App Name");
                targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShareIntents.add(targetedShare);
            }
        }
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    }
}
catch(Exception e){
     Log.v("VM","Exception while sending image on" + nameApp + " "+  e.getMessage());
 }
}