I am making android wallpaper app. At the start, I didn't use imagecropper and background image for android was very perfect. Then I wanted to add ImageCropper function and I noticed the problem: Image that I got from ImageCropper was very defected and bad quality. I am sure that the problem is here: (Performcrop method(starts activity for result))
public void performCrop(Uri picUri, Bitmap qq){
try {
utils = new Utils(getApplicationContext());
int height, width;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
File f= new File(utils.saveImageToSDCard(qq));
f.createNewFile();
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate X and Y
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri.fromFile(f));
startActivityForResult(cropIntent, 1);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
} catch (IOException e) {
e.printStackTrace();
}
}
OnActivityResult:
#Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
//get the cropped bitmap
if (data != null) {
Bundle extras = data.getExtras();
int height, width;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
utils = new Utils(getApplicationContext());
Bitmap thePic = (Bitmap) (extras.getParcelable("data"));
Bitmap krutoi = Bitmap.createBitmap(thePic, 0, 0, width, height);
utils.setAsWallpaper(krutoi);
}
}
}
Here OnClick listener for SetWallpaperButton:
llDownloadWallpaper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
utils = new Utils(getApplicationContext());
bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
utils.saveImageToSDCard(bitmap);
}
});
saveImageToSdCard(Utils.java):
public String saveImageToSDCard(Bitmap bitmap) {
File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
pref.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(
_context,
_context.getString(R.string.toast_saved).replace("#",
"\"" + pref.getGalleryName() + "\""),
Toast.LENGTH_SHORT).show();
Log.d(TAG, "Wallpaper saved to: " + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_saved_failed),
Toast.LENGTH_SHORT).show();
}
return file.getAbsolutePath();
}
setAsWallpaper(Utils.java):
public void setAsWallpaper(Bitmap bitmap) {
try {
WallpaperManager wm = WallpaperManager.getInstance(_context);
wm.setBitmap(bitmap);
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
Related
Its open the "choose from galary" option but i dont want this. I want directly showing the croping option on image. I have used intent for that
// Crop.java
private String defaultImagePath;
final int PIC_CROP = 1;
private Uri mCropImagedUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.crop);
cropImage = (ImageView) this.findViewById(R.id.imageView4);
seekBarCrop = (SeekBar) findViewById(R.id.seekBarCrop);
Bundle b = getIntent().getExtras();
String imagePath = b.getString("imagePath");
defaultImagePath = imagePath;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bitmap = BitmapFactory.decodeFile(imagePath, options);
cropImage.setImageBitmap(bitmap);
cropImageActivity(defaultImagePath, cropImage);
}
private void cropImageActivity(String path, View cropImage) {
try {
mCropImagedUri = Uri.parse(path);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(mCropImagedUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
cropIntent.putExtra("return-data", false);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast
.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
cropImage.setImageBitmap(selectedBitmap);
}
}
}
I'm actually new in android but I managed to take a picture with my app and this is how I take a picture and save it... The problem is that I need to resize it before saving it on the phone... But I can't figure out how to to that.. I've googled my problem but the only thing I found was with bitmap pictures and that's not my case I guess..
Here is the code I'm using to take the picture:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"GUASTO" + System.currentTimeMillis() + ".jpg"); /
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
startActivityForResult(intent, SCATTA_FOTO);
Thank you!!
public static int PIC_CROP=81;
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(ImageUri, "image/*");
//set crop properties
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 640);
cropIntent.putExtra("outputY", 640);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
and in onActivityResult()
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode==PIC_CROP)
{
if(resultCode==RESULT_OK)
{
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bmp = extras.getParcelable("data");
saveCropPhoto(bmp);
}
Toast.makeText(getApplicationContext(), "picture cropped",Toast.LENGTH_SHORT).show();
}
}
}
saveCropPhoto() method
public void saveCropPhoto(Bitmap bmp)
{
Toast.makeText(getApplicationContext(), "in save",Toast.LENGTH_SHORT).show();
String dir = Environment.getExternalStorageDirectory().toString() + "/folderName/";
File newdir = new File(dir);
newdir.mkdirs();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
File imageFileName = new File(newdir, "crop_"+date.toString() + ".jpg");
try
{
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
out = null;
if(tempFile.exists())
tempFile.delete();
} catch (Exception e)
{
e.printStackTrace();
}
}
I am passing image url to the following method
private void performCrop(Uri imageUri){
try {
Intent intent = new Intent("com.android.camera.action.CROP");
// intent.setType("image/*");
intent.setDataAndType(imageUri, "image/*");
List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size >= 0) {
intent.setData(imageUri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
System.out.println("before startActivityForResult");
try{
startActivityForResult(i, 2);
}catch(Exception e){
e.printStackTrace();
}
}
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support the crop action!";
//Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
//toast.show();
}
}
some of images are cropped but some image are not. When I crop image then my app's activity is closed and application starts it's previous or launcher activity so what is the problem??
the main issue is I am not getting any warning or error in log cat, and when I debug it the the till "System.out.println("before startActivityForResult");" the program excutes, that means it does not goes in or calling onActivityResult().
here is onActivityResult method
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode ==2 && resultCode == getActivity().RESULT_OK){
System.out.println("inside logic...");
try{
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap bmp = extras.getParcelable("data");
imageView.setImageBitmap(bmp);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
this code works successfully for me.try it
private static final int CAMERA_REQUEST = 1;
public static final int MEDIA_TYPE_IMAGE = 1;
final int PIC_CROP = 12;
Button btnCamera;
ImageView iv;
Uri picUri;
static File mediaFile, sendFile;
btnCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onImgProfile();
}
});
void onImgProfile() {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
picUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(captureIntent, CAMERA_REQUEST);
}
private Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), "MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
#SuppressWarnings("unchecked")
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
// if (Build.VERSION.SDK_INT < 19) {
try {
if (mediaFile.exists()) {
performCrop();
// new SavePhotoData().execute();
}
} catch (Exception e) {
// TODO: handle exception
}
// }
} else if (requestCode == 11) {
try {
picUri = data.getData();
Log.i("uri", "" + picUri);
performCrop();
} catch (Exception e) {
// TODO: handle exception
}
} else if (requestCode == PIC_CROP) {
// get the returned data
try {
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
// retrieve a reference to the ImageView
// display the returned cropped image
iv.setImageBitmap(thePic);
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
sendFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".png");
FileOutputStream fOut = new FileOutputStream(sendFile);
thePic.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (resultCode == 3) {
Bundle b = data.getExtras();
b.getString("msg");
}
};
private void performCrop() {
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
} catch (Exception e) {
// TODO: handle exception
}
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(getApplicationContext(), errorMessage,
Toast.LENGTH_SHORT);
toast.show();
}
}
this code works for not only kitkat but all cersion of android
Android all devices does not have a cropping intent according #CommonsWare http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html
so better is to use libraries
some of them are:
https://github.com/jdamcd/android-crop
https://github.com/IsseiAoki/SimpleCropView
https://android-arsenal.com/details/1/3054
In my app I take picture from camera and preview it. My app takes picture in landscape mode so that I changed it to portrait mode using exifInterface but I get stretched image.
How can I redraw my Image with out stretched image from the bitmap. if I take picture in landscape mode, it looks good.
this is my code for camera part:
public class TakePic extends Fragment {
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.add_spot_2, container, false);
....
upload.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
String path = Environment.getExternalStorageDirectory()
+ "/pic";
File photopath = new File(path);
if (!photopath.exists()) {
photopath.mkdir();
}
imagePath = new File(photopath, "pic"
+ System.currentTimeMillis() + ".png");
DataPassing.imagePath = imagePath;
if(imageUri==null){
Log.e("Image uri is null", "Image uri is null 1");
}
else{
Log.e("Image uri is NOT null", "Image uri is NOT null 1");
}
Log.e("file path ", imagePath.getAbsolutePath());
imageUri = Uri.fromFile(imagePath);
DataPassing.imageUri = imageUri;
if(imageUri==null){
Log.e("Image uri is null ", "Image uri is null 2");
}
else{
Log.e("Image uri is NOT null", "Image uri is NOT null 2");
}
Log.e("uri file path ", imageUri.getPath());
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(imagePath));
startActivityForResult(intent, 100);
}
});
........
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#SuppressWarnings({ "deprecation" })
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
if (DataPassing.imageUri == null) {
Toast.makeText(getActivity(), "Please retry",
Toast.LENGTH_SHORT).show();
} else {
Bitmap bm = readBitmap(DataPassing.imageUri);
int or = 0;
try {
or = resolveBitmapOrientation(DataPassing.imagePath);
Log.e("int", String.valueOf(or));
} catch (IOException e) {
e.printStackTrace();
}
if(or==1){
image.setBackgroundDrawable(new BitmapDrawable(bm));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] array = stream.toByteArray();
AddNewSpotValues.comm_2_picture_path = array;
DataPassing.addspot2_bm = bm;
}
else{
int w = bm.getWidth();
int h = bm.getHeight();
Log.e("w & h", ""+w + " & " + h);
Matrix mtx = new Matrix();
mtx.postRotate(90);
Bitmap rbm = Bitmap.createBitmap(bm, 0, 0, w, h, mtx, true);
// Bitmap rbm = Bitmap.createBitmap(bm, 0, 0, 200,150, mtx, true);
image.setBackgroundDrawable(new BitmapDrawable(rbm));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rbm.compress(Bitmap.CompressFormat.PNG, 100, stream);
DataPassing.addspot2_bm = rbm;
byte[] array = stream.toByteArray();
AddNewSpotValues.comm_2_picture_path = array;
}
}
}
}
#Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();
}
private Bitmap readBitmap(Uri selectedImage) {
try {
File f = new File(selectedImage.getPath());
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 100;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
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) {
}
return null;
}
private int resolveBitmapOrientation(File bitmapFile) throws IOException {
ExifInterface exif = null;
exif = new ExifInterface(bitmapFile.getAbsolutePath());
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
}
}
You might need to open your camera in portrait mode instead of manipulating the image itself accepted answer here show you how to do it
In my application, I need to take images from gallery/camera, crop those images, then save the cropped images some where else. The below code does most of that, but cannot crop images to my liking. Using the below code, I can crop images using 4 coordinates, top, bottom, left and right side of image middle coordinates; but I need to crop using 8 coordinates. This image shows what I mean.
public class MainActivity extends Activity {
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
private static final int PRESS_OK = 3;
ImageView imgview;
String m_path;
Bitmap m_thePic;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
Button buttonOk = (Button) findViewById(R.id.btn_ok);
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Images/");
folder.mkdirs();
buttonCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// call android default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
buttonOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String m_path = Environment.getExternalStorageDirectory().toString();
File m_imgDirectory = new File(m_path + "/Images/");
File m_file = new File(m_path);
String m_fileid = "nm_tech" + System.currentTimeMillis() + "";
m_file = new File(m_path, "/Images/" + m_fileid + ".jpg");
Uri outputFileUri = Uri.fromFile(m_file);
Intent intent = new Intent(MainActivity.this,
ImageGalleryDemoActivity.class);
intent.putExtra("image", m_fileid);
startActivity(intent);
// startActivityForResult(intent,PRESS_OK);
// call android default camera
// Toast.makeText(getApplicationContext(), ,1234).show();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras = data.getExtras();
Bitmap m_thePic = extras.getParcelable("data");
String m_path = Environment.getExternalStorageDirectory().toString();
File m_imgDirectory = new File(m_path + "/Images/");
if (!m_imgDirectory.exists()) {
m_imgDirectory.mkdir();
}
OutputStream m_fOut = null;
File m_file = new File(m_path);
m_file.delete();
String m_fileid = "nm_tech" + System.currentTimeMillis() + "";
m_file = new File(m_path, "/Images/" + m_fileid + ".jpg");
try {
if (!m_file.exists()) {
m_file.createNewFile();
}
m_fOut = new FileOutputStream(m_file);
Bitmap m_bitmap = m_thePic.copy(Bitmap.Config.ARGB_8888, true);
m_bitmap.compress(Bitmap.CompressFormat.PNG, 100, m_fOut);
m_fOut.flush();
m_fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
m_file.getAbsolutePath(),
m_file.getName(),
m_file.getName());
} catch (Exception p_e) {
}
if (requestCode == PICK_FROM_CAMERA) {
if (extras != null) {
// Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(m_thePic);
}
}
if (requestCode == PICK_FROM_GALLERY) {
// Bundle extras2 = data.getExtras();
if (extras != null) {
imgview.setImageBitmap(m_thePic);
}
}
if (requestCode == PRESS_OK) {
Bundle extras11 = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
/*
* Bitmap photo = extras.getParcelable("data");
* imgview.setImageBitmap(photo); Intent n=new
* Intent(getApplicationContext(),ImageGalleryDemoActivity.class);
* n.putExtra("data",photo); startActivity(n);
*/
}
}
}
Here is a simple way for you to create minimum needed rect from an array of coordinates
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Rect r = getRect(new int[]{10, 10, 20, 20, 30, 30}, new int[]{20, 100, 10, 110, 20, 100});
System.out.println(r.left + " " + r.top + " " + r.bottom + " " + r.right);
}
public Rect getRect(int[] x, int y[]){
Rect r = new Rect();
// Set the first coordinate, in order not to include 0, 0
r.set(x[0], y[0], x[0], y[0]);
for(int i = 1; i < x.length; i++){
r.union(x[i], y[i]);
}
return r;
}
EDIT: look you have 8 points, just call this getRect like this
Rect rectToDraw = getRect(new int{yourx1, yourx2, yourx3, yourx4, yourx5, yourx6, yourx7, yourx8,}, new int{youry1, youry2, youry3, youry4, youry5, youry6, youry7, youry8});
You can pass this function ass many points you want, not just 8
Hope this helps and enjoy your work