La idea de la aplicación, es que quiero tomar una foto y guardarla con cierto nombre en una ubicación especifica, hasta el momento, tengo: He tomado la foto, y la pongo en un imageview
, ahora, voy a guardar la imagen. Pero cuando lo guardo, todo el código se va directamente a la parte de: "Error during image saving".
Los permisos que le otorgue a la app son:
<uses-feature android:name="android.hardware.camera"android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Espero si me pueden ayudar, o brindar otro codigo.
Aca esta todo el codigo:
package com.agustincanoalvarez.myapplication3;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
Button btnCaptura,btnGuardar;
ImageView imageView;
int contador=0;
Bitmap imageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnCaptura.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PictureIntent();
}
});
btnGuardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Guardar();
}
});
}
private void init()
{
imageView =(ImageView)findViewById(R.id.imgv);
btnCaptura =(Button)findViewById(R.id.btnCap);
btnGuardar =(Button)findViewById(R.id.btnGuardar);
}
private void PictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
private void Guardar()
{
contador=contador+1;
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File file = new File (sdCardDirectory,"ensayo"+String.valueOf(contador)+".png");
Toast.makeText(getApplicationContext(), "ensayo"+String.valueOf(contador)+".png",
Toast.LENGTH_LONG).show();
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
}