Pages

Thursday, June 26, 2014

Capture image from camera in android


To capture image from android first fire an intent.
       
        private static final int REQUEST_CODE_IMAGE = 101;

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CODE_IMAGE);


//  get data on on Activity Result use the below code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
 if (requestCode == REQUEST_CODE_IMAGE && resultCode == RESULT_OK) {

   if (data != null) {
    if (data.getExtras().get("data") != null) {
 
     Uri fileUri = data.getData();
     String filePath = getRealPathFromURI(fileUri,
       MediaStore.Images.Media.DATA);
     File file = new File(filePath);
     String fileName = file.getName();


     try {
      Bitmap photo = (Bitmap) data.getExtras().get("data");
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo.compress(Bitmap.CompressFormat.PNG, 100, stream);



     } catch (Exception ex) {

     }
    }
   }
  }

Show or Read PDF in Android


To Show PDF file in your android phone first save it in your assets folder than read PDF file by using below two methods.
These methods first store your pdf in local storage than show it from local storage.

private void CopyReadAssets(String fileName) {
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), fileName);
try {
in = assetManager.open(fileName);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/" + fileName),
"application/pdf");

startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}

Saturday, February 15, 2014

How to get screen dimensions in android



There is Display class to get the display of screen in android.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//getSize()  method is introduced in API level 13.
int width = size.x;
int height = size.y;

Before API  level 13 below method are used.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); //this method is deprecated now
int height = display.getHeight();//this method is deprecated now



Wednesday, February 12, 2014

Example to show Animated GIF image in Android

Android  doesnot support GIF image simply you should use WebView  or other thing to run gif image.
I use WebView to implement the GIF image .


First create a GIFView by extending webview.

public class GIFView extends WebView {

public static final String HTML_FORMAT = "<html><body style=\"text-align: center;  vertical-align:                       right;background-color: transparent;\"><img src = \"%s\" /></body></html>";

public GIFView(Context context, String fileUrl) throws IOException {
  super(context);


final String html = String.format(HTML_FORMAT, fileUrl);

setBackgroundColor(Color.TRANSPARENT);
loadDataWithBaseURL("", html, "text/html", "UTF-8", "");

}

}


Create your activity class here.

public class MainActivity extends Activity {

private WebView webView;
// private ProgressBar pd;
private LinearLayout llProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


      llProgress = (LinearLayout) findViewById(R.id.ll_progress);
try {

//                give your gif image name here(example.gif).
GIFView gif = new GIFView(this,
"file:///android_asset/example.gif");
llProgress.addView(gif);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


here is your   activity_main   file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/ll_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >
    </LinearLayout>

</RelativeLayout>


Tuesday, February 11, 2014

Example to create Round Shape image bitmap android



public static Bitmap getRoundedShape(Bitmap scaleBitmapImage,
int targetWidth, int targetHeight, Context context) {
// TODO Auto-generated method stub
// int targetWidth = width;
// int targetHeight = height;

targetHeight = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, targetHeight, (context)
.getResources().getDisplayMetrics());
targetWidth = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, targetWidth, (context)
.getResources().getDisplayMetrics());

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth) / 2, ((float) targetHeight) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
Log.d("", "In process: width" + targetWidth + " height" + targetHeight);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}