In my activity onCreate I set a onCheckedChangeListener to a radio group.
At some point I need to programmatically uncheck any checked radio button within the radio group so I unset the listener, uncheck the button, and set the listener back to prevent it from firing when programmatically unchecking the button.
When first checking the radio button, it takes a photo trough an intent. In the onActivityResult part, I uncheck the button if the user didn't take a photo. After doing this, the radio button unchecks itself whenever it's checked. How can I fix this problem?
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist_vestuarios);
instanciar_objetos();
instanciar_listeners();
}
private void instanciar_objetos(){
ctx = getApplicationContext();
rg_1 = findViewById(R.id.radio_1_1);
}
private void instanciar_listeners(){
listener_radio_groups = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
last_rg = radioGroup;
seleccionar_radio((RadioButton) findViewById(last_rg.getCheckedRadioButtonId()));
}
};
rg_1.setOnCheckedChangeListener(listener_radio_groups);
}
private void seleccionar_radio(RadioButton rb){
String tag = (String) rb.getTag();
String[] s = tag.split("_");
seccion = s[0];
item = s[1];
radio = s[2];
if(radio.equalsIgnoreCase("1")){
//MARCÓ SI, MANDARLE CON LA FOTO
fotografia();
}
else{
//MARCO OTRA OPCION, PEDIR TEXTO
}
}
public void fotografia(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
try{
Uri photoURI = FileProvider.getUriForFile(ctx,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 1);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
String imageFileName = timeStamp;
String root = Environment.getExternalStorageDirectory().toString() + "/hrtech/hites/originales";
File storageDir = new File(root, "/"+seccion+"/"+item);
storageDir.mkdirs();
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
// Show the thumbnail on ImageView
Uri imageUri = Uri.parse(mCurrentPhotoPath);
final File file = new File(imageUri.getPath());
// ScanFile so it will be appeared on Gallery
MediaScannerConnection.scanFile(ctx,
new String[]{imageUri.getPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
}
else{
Toast.makeText(ctx,"Obligatoriamente debe tomar una foto para marcar \"SI\" en este ítem.",Toast.LENGTH_LONG).show();
last_rg.setOnCheckedChangeListener(null);
RadioButton aux = (RadioButton) findViewById(last_rg.getCheckedRadioButtonId());
aux.toggle();
last_rg.setOnCheckedChangeListener(listener_radio_groups);
}
}
Related
I have an app in which I try to capture user images from camera and also from gallery. The image capture works fine and the image is visible in the ImageView but when I restart the app the image disappears as if it wasn't saved.
public class MainActivity extends AppCompatActivity {
ImageView imageView;
int REQUEST_CAMERA =1;
int SELECT_FILE = 0;
public String photoFileName;
File photoFile = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = findViewById(R.id.imageView);
final FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
selectImage();
}
});
}
public void selectImage(){
final CharSequence [] items = {"Camera","Gallery","Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
#SuppressLint("IntentReset")
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_CAMERA);
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
"Photo file can't be created, please try again",
Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"com.example.android.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
}else if (items[i].equals("Gallery")){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select File"),SELECT_FILE);
galleryAddPic();
}else if (items[i].equals("Cancel")){
dialogInterface.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK){
Bundle bundle = null;
if (data != null) {
bundle = data.getExtras();
}
Bitmap bitmap = null;
if (bundle != null) {
bitmap = (Bitmap) bundle.get("data");
}
imageView.setImageBitmap(bitmap);
}else if (requestCode == SELECT_FILE){
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(String.valueOf(photoFile));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
photoFileName = image.getAbsolutePath();
return image;
}
}
While restart the app image is not showing becuase of all variable initialize again, for showing that you have to store it path locally.
I am trying to capture an image and go to next intent with the image. This following code going to Camera Intent. But not going to second intent. Its returning java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity error. that means there is no data going to onResultActivity. If I put data!=null in the argument then its returning to 1st Activity. What I am missing? I have tested the mCurrentPhotoPath. Its returning the filePath.
String shopName;
double latitude, longitude;
private int REQUEST_IMAGE_CAPTURE = 0;
private Bitmap imageBitmap;
String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 0;
private File createImageFile() throws IOException {
// Create an image file name
#SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (photoFile != null) {
//Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"com.marketaccess.data.collector.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode != RESULT_CANCELED) {
Uri filePath = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] imageResource = stream.toByteArray();
Intent sendDataIntent = new Intent(MainActivity.this, SendData.class);
Bundle extras = new Bundle();
sendDataIntent.putExtras(extras);
sendDataIntent.putExtra("imageResource", imageResource);
startActivity(sendDataIntent);
}
}
I have tested the Manifest fileprover block. It seems like ok and returing path.
I have a CardActivity that opens a CameraActivity. I have an imagebutton I press and then the native camera app opens and I can take a picture. I try to pass that back to my CardActivity using an intent with a ByteArray. But it gives me a blank white screen. It doesnt insert anything into the imageview. The Bitmap element is not null, it has something.
This is my switch for starting the camera activity and setting image:
switch (v.getId()) {
case R.id.pButton1:
Intent cam = new Intent(CardActivity.this, CameraActivity.class);
startActivity(cam);
returnImage2();
mImageView = (ImageView)findViewById(R.id.imageView);
mImageView.setImageBitmap(bitmap);
mImageView.setRotation(180);
break;
This is my returnImage2();
public void returnImage2() {
try {
bitmap = BitmapFactory.decodeStream(this.openFileInput("myImage"));
}
catch (Exception e) {
e.printStackTrace();
}
}
This is my cameraActivity:
public class CameraActivity extends Activity {
private String mCurrentPhotoPath;
private ImageView mImageView;
private Bitmap mImageBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dispatchTakePictureIntent();
}
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
System.out.println("ERR CANNOT CREATE FILE");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
private File createImageFile2() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
cImageFromBitmap(mImageBitmap);
//mImageView = (ImageView)findViewById(R.id.imageView);
//mImageView.setImageBitmap(mImageBitmap);
//mImageView.setRotation(180);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String cImageFromBitmap(Bitmap bitmap){
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
}
I noticed you are using REQUEST_TAKE_PHOTO for starting your activity
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
but you are checking for REQUEST_IMAGE_CAPTURE on onActivityResult
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
ALSO:
I don't know why you are going through all this work to create a file. When you use ACTION_IMAGE_CAPTURE the bitmap itself comes back in the result intent:
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try {
mImageBitmap = (Bitmap) data.getExtras().get("data");
I need help with my issue of saving a picture . I am using adobe photo sdk to edit my image. Documentation link(https://creativesdk.adobe.com/docs/android/#/articles/imageediting/index.html). They say use .withOutput(Uri) to save the image which i created but get my image with an error.
public class MainActivity extends AppCompatActivity {
private static final int IMG_CODE_EDIT = 263;
private Button mPickbtn;
private Button mCapturebtn;
private static int RESULT_LOAD_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private String mCurrentPhotoPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPickbtn = (Button) findViewById(R.id.pick_image);
mCapturebtn = (Button) findViewById(R.id.button2);
Intent cdsIntent = AdobeImageIntent.createCdsInitIntent(getBaseContext(), "CDS");
startService(cdsIntent);
}
public void takePicture(View view) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
public void onClick(View view) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Gets the gallery image uri
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);
cursor.close();
editPic(selectedImage);
}
//picture from camera
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri mPicTaken = data.getData();
editPic(mPicTaken);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
if (resultCode == RESULT_OK) {
switch (requestCode) {
/* 4) Make a case for the request code we passed to startActivityForResult() */
case 263:
/* 5) Show the image! */
Uri editedImageUri = data.getData();
Intent intent = new Intent("com.ayyogames.photoapp.Share");
intent.putExtra("imageUri", editedImageUri);
startActivity(intent);
break;
}
}
}
public void editPic(Uri uri) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.ayyogames.photoapp.fileprovider",
photoFile);
Intent intent = new AdobeImageIntent.Builder(this)
.setData(uri)
.withOutputSize(MegaPixels.Mp10)
.withOutputQuality(100)
.withOutput(photoURI)
.build();
startActivityForResult(intent, IMG_CODE_EDIT);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
Share Class
public class Share extends AppCompatActivity {
private ImageView mEditedImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
mEditedImageView = (ImageView) findViewById(R.id.editedImageView);
Intent intent = getIntent();
Uri uri = intent.getParcelableExtra("imageUri");
mEditedImageView.setImageURI(uri);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return true;
}
}
The Image Editor developer guide currently says that you should pass a Uri to withOutput(), but this is incorrect.
Try passing a File:
/* 1) Change the argument to your desired location */
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
.setData(imageUri)
.withOutput(file) /* 2) Pass the File here */
.build();
Your argument to the File constructor should be altered to whatever location you desire.
I'm trying to make a simple app:
I made a button, its onClick should call the camera intent, save the picture in the internal storage and then i want to put the picture in a custom listview gallery... for now I haven't made the listview because I'm having problems with the intent. the camera doesn't open and I don't know why even if the right permissions are in the manifest file
any help is appreciated, I've tried a lot of tutorials but I can't find the problem.
public class MainActivity extends Activity implements View.OnClickListener {
public static final String LOG_TAG = "LOG_TAG" ;
public static final int CAMERA_REQUEST = 1; /* ci serve la richiesta per la camera*/
private Button btCamera;
private Button btSavePicture;
private ImageView imageViewShowPicture;
private File imageFile;
/*attributi per salvare foto*/
private String savePicturePath;
/*_____________________________________________________________________________________________oncreate*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Collego oggetti alla grafica*/
btCamera = (Button)this.findViewById(R.id.btCamera);
btSavePicture = (Button) this.findViewById(R.id.btSalvaFoto);
this.imageViewShowPicture = (ImageView) this.findViewById(R.id.ivMostraFoto);
/*listener*/
btCamera.setOnClickListener(this);
btSavePicture.setOnClickListener(this);
}
/*___________________________________________________________________________________________metodo TAKE A PHOTO*/
private void startTakeAPictureIntent(){
Intent takeAPictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
}}
/*___________________________________________________________________________________________metodo crea image file*/
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
savePicturePath = "file:" + image.getAbsolutePath();
return image;
}
/*______________________________________________________________________________________metodo aggiungi alla gallery*/
private void addPicToGallery() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(savePicturePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
/*______________________________________________________________________________________metodo decodificare un'immagine scalata*/
private void setPic() {
// Get the dimensions of the View
int targetW = imageViewShowPicture.getWidth();
int targetH = imageViewShowPicture.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePicturePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(savePicturePath, bmOptions);
imageViewShowPicture.setImageBitmap(bitmap);
}
/*_____________________________________________________________________________________________onclick*/
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btCamera: {
Log.d(LOG_TAG, "Ho premuto btcamera");
Toast.makeText(MainActivity.this, "Camera clicked", Toast.LENGTH_SHORT).show();
startTakeAPictureIntent();
/*Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, RICHIESTA_FOTOCAMERA);*/
} break;
case R.id.btSalvaFoto:{
Log.d(LOG_TAG, "Ho premuto btsalvafoto");
Toast.makeText(MainActivity.this, "salvafoto clicked", Toast.LENGTH_SHORT).show();
}
}
}
/*_____________________________________________________________________________________________onActivity result*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { /* se la richiesta mi da risultati*/
Bundle extras = data.getExtras();
Bitmap immagineBitmap = (Bitmap)extras.get("data"); /* prendi gli extra */
imageViewShowPicture.setImageBitmap(immagineBitmap); /*setto la imageview per mostrarla*/
}
}
}// chiude activity
Please try to add following permission and see.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Please refer to https://developer.android.com/guide/topics/media/camera.html#manifest to see anything other you need to declare.