Showing posts with label save image in android. Show all posts
Showing posts with label save image in android. Show all posts

Load Images From Phone in Android by Specifying Image Path

Load Images From Phone in Android

Sample Code


private Bitmap getBitmap(String imageFilePath)
{



// Load up the image's dimensions not the image itself

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

bmpFactoryOptions.inJustDecodeBounds = true;

Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight/ (float) DISPLAYHEIGHT);

int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth/ (float) DISPLAYWIDTH);

Log.v("HEIGHTRATIO", "" + heightRatio);

Log.v("WIDTHRATIO", "" + widthRatio);

// If both of the ratios are greater than 1, one of the sides of
// the image is greater than the screen


if (heightRatio > 1 && widthRatio > 1) {

if (heightRatio > widthRatio) {

// Height ratio is larger, scale according to it

bmpFactoryOptions.inSampleSize = heightRatio;

} else {

// Width ratio is larger, scale according to it

bmpFactoryOptions.inSampleSize = widthRatio;

}

}

// Decode it for real
bmpFactoryOptions.inJustDecodeBounds = false;

bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);

return bmp;
}

How to Take Screenshot Programatically in Android


 // Step1: First Create the base view

               View vi = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);

                vi.setDrawingCacheEnabled(true);

  //   Step2: Create a Bitmap

                Bitmap b = vi.getDrawingCache();

//     step3: Save This bitmap







Convert Bitmap to Byte Array and Save

                             ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

                             try {
OutputStream imageFileOS = getContentResolver()
.openOutputStream(imageFileUri);
imageFileOS.write(byteArray);
imageFileOS.flush();
imageFileOS.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
 

Popular Posts

About Me

Only For Sharing Android Related Things...

Facebook Fan Page