Select Big size image from SDCard or from Capture from Camera and display getting VM Budget Error so try below code
to select image from sdcard
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Take picture from camera
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
File mFile = new File(Environment.getExternalStorageDirectory() + "/My_images");
if(!mFile.exists()) {
mFile.mkdirs();
}
mFile = new File(mFile.getAbsolutePath() + "/temp.jpg");
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
startActivityForResult(i, CAMERA_RESULT);
} else {
Toast.makeText(getBaseContext(), "Camera is not available",
Toast.LENGTH_LONG).show();
}
after get image in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null,
null, null);
cursor.moveToFirst();
int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//strimagepath = picturePath;
File f = new File(picturePath);
String strimagepath = f.getAbsolutePath();
loadAndDisplayImage(f);
}
if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {
final File mFile = new File(Environment.getExternalStorageDirectory() + "/My_images/temp.jpg");
if(!mFile.exists()) {
Toast.makeText(getBaseContext(), "Image not captured successfully.", Toast.LENGTH_SHORT).show();
return;
}
String strimagepath = mFile.getAbsolutePath();
loadAndDisplayImage(mFile);
}
}
new loadAndDisplayImage function here
private void loadAndDisplayImage(File mFile) {
new AsyncTask<File, Void, Bitmap>() {
byte[] bitmapdata;
ProgressDialog pbr;
protected void onPreExecute() {
System.gc();
pbr = new ProgressDialog(SaveAngle.this);
pbr.setMessage("Loading...");
pbr.show();
};
@Override
protected Bitmap doInBackground(File... params) {
Bitmap mBitmap = decodeFile(params, 256);
return null;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
if(pbr != null && pbr.isShowing()) {
pbr.dismiss();
}
imguser.setScaleType(ScaleType.CENTER_INSIDE);
imguser.setImageBitmap(result);
}
};
}.execute(mFile);
}
decodeFile function is here
public static Bitmap decodeFile(File f, int size) {
System.out.println("Decoding file");
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
//final int REQUIRED_SIZE = 70;
//final int REQUIRED_SIZE = 300;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < size || height_tmp / 2 < size)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
System.out.println("Error :: " + e.getMessage());
}
return null;
}
to select image from sdcard
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Take picture from camera
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
File mFile = new File(Environment.getExternalStorageDirectory() + "/My_images");
if(!mFile.exists()) {
mFile.mkdirs();
}
mFile = new File(mFile.getAbsolutePath() + "/temp.jpg");
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
startActivityForResult(i, CAMERA_RESULT);
} else {
Toast.makeText(getBaseContext(), "Camera is not available",
Toast.LENGTH_LONG).show();
}
after get image in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null,
null, null);
cursor.moveToFirst();
int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//strimagepath = picturePath;
File f = new File(picturePath);
String strimagepath = f.getAbsolutePath();
loadAndDisplayImage(f);
}
if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {
final File mFile = new File(Environment.getExternalStorageDirectory() + "/My_images/temp.jpg");
if(!mFile.exists()) {
Toast.makeText(getBaseContext(), "Image not captured successfully.", Toast.LENGTH_SHORT).show();
return;
}
String strimagepath = mFile.getAbsolutePath();
loadAndDisplayImage(mFile);
}
}
new loadAndDisplayImage function here
private void loadAndDisplayImage(File mFile) {
new AsyncTask<File, Void, Bitmap>() {
byte[] bitmapdata;
ProgressDialog pbr;
protected void onPreExecute() {
System.gc();
pbr = new ProgressDialog(SaveAngle.this);
pbr.setMessage("Loading...");
pbr.show();
};
@Override
protected Bitmap doInBackground(File... params) {
Bitmap mBitmap = decodeFile(params, 256);
return null;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
if(pbr != null && pbr.isShowing()) {
pbr.dismiss();
}
imguser.setScaleType(ScaleType.CENTER_INSIDE);
imguser.setImageBitmap(result);
}
};
}.execute(mFile);
}
decodeFile function is here
public static Bitmap decodeFile(File f, int size) {
System.out.println("Decoding file");
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
//final int REQUIRED_SIZE = 70;
//final int REQUIRED_SIZE = 300;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < size || height_tmp / 2 < size)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
System.out.println("Error :: " + e.getMessage());
}
return null;
}
No comments:
Post a Comment
Comments