Я пытался вставить в манифест
android: configChanges = "orientiation | screenSize"
, но это никак не отразилось
public class ReportFragment extends Fragment {
Integer REQUEST_CAMERA = 1, SELECT_FILE = 0;
private static final int CAMERA_PHOTO = 111;
private Uri imageToUploadUri;
private File f;
ImageView ivImage;
Intent chooserIntent = null;
private void SelectImage(){
final CharSequence[] items={"Camera","Galleria", "Cancella"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Aggiungi immagine");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (items[i].equals("Camera")) {
if(f!= null){
f.delete();
}
chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
imageToUploadUri = Uri.fromFile(f);
startActivityForResult(chooserIntent, 1);
} else if (items[i].equals("Galleria")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select File"), 2);
} else if (items[i].equals("Cancella")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
if (imageToUploadUri != null) {
Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
if (reducedSizeBitmap != null) {
textPhoto.setText("Foto allegata con successo!");
textPhoto.setTextColor(Color.parseColor("#008000"));
ivImage.setImageBitmap(reducedSizeBitmap);
flagImage = true;
}
else {
Toast.makeText(getActivity(), "Error while capturing Image", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getActivity(), "Error while capturing Image", Toast.LENGTH_LONG).show();
}
} else if (requestCode == 2) {
Uri selectedImageUri = data.getData();
textPhoto.setText("Foto allegata con successo!");
textPhoto.setTextColor(Color.parseColor("#008000"));
ivImage.setImageURI(selectedImageUri);
flagImage = true;
}
}
}
private Bitmap getBitmap(String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = getActivity().getApplicationContext().getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = getActivity().getApplicationContext().getContentResolver().openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e("", e.getMessage(), e);
return null;
}
}