In my application I make the selection of an image of the gallery, then I save it in the bank, but if the image has a high quality the app does not save, besides saving it stops working, has some way of save that image in the bank even if it is of good quality or does not let the user save that image and send a message to it?
public class Horarios extends AppCompatActivity {
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private Button btnCamera;
private String Chave;
BancoDeDados db = new BancoDeDados(this);
SQLiteDatabase banco;
Bitmap bitmap;
private ImageView imageView;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.horarios);
btnSelect = (Button) findViewById(R.id.btnSelect);
btnCamera = (Button) findViewById(R.id.btnCamera);
imageView = (ImageView) findViewById(R.id.imageView);
fab = (FloatingActionButton) findViewById(R.id.fabH);
btnSelect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Chave = "Selecionar";
galleryIntent();
}
});
btnCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Chave = "Camera";
cameraIntent();
}
});
fab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
insertImg(bitmap);
Toast.makeText(Horarios.this, "Imagem Salva!", Toast.LENGTH_SHORT).show();
finish();
}
});
//carregar imagem
byte[] imageFinal = getImage();
if(imageFinal != null){
try {
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageFinal,0,imageFinal.length));
imageView.invalidate();
} catch(Exception e) {
Toast.makeText(Horarios.this, "erro:" +e, Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(Chave.equals("Camera"))
cameraIntent();
else if(Chave.equals("Selecionar"))
galleryIntent();
}
break;
}
Toast.makeText(Horarios.this, "testetando o codigo 111111", Toast.LENGTH_LONG).show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
bitmap = null;
if (data != null) {
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
imageView.setImageBitmap(bitmap);
fab.setVisibility(View.VISIBLE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
private void onCaptureImageResult(Intent data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
fab.setVisibility(View.VISIBLE);
}
public void insertImg(Bitmap img ) {
byte[] data = getBitmapAsByteArray(img);
banco = db.getWritableDatabase();
ContentValues content = new ContentValues();
content.put("imagem", data);
banco.insert("Horarios", null, content);
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
public byte[] getImage(){
byte[] result = null;
banco = db.getReadableDatabase();
String qu = "select imagem from Horarios";
Cursor cur = banco.rawQuery(qu, null);
if (cur.moveToLast()){
result = cur.getBlob(0);
cur.close();
}
if (cur != null && !cur.isClosed()) {
cur.close();
}
return result;
}
}