1
In my application I make the selection of an image from the gallery, then I save it in the bank, but if the image has a high quality the app does not save, besides not saving it to work, has some way to save this image in the bank even if it is with a good quality or not let the user save that image and send a message to him ?
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;
}
}
Friend, without you publishing your code it will be impossible to answer this question correctly. I imagine the problem is memory consumption when loading the full bitmap without downsampling it (something automatically performed if you use an image library such as Glide). Post your code, or this question may even end up being closed by moderators! At least the part that reads the image and records it. Another thing: do not store image in the database, but rather on file and just save its path in the database.
– Loudenvier
so if you have the code or some link that helps me save only the way and can send me I thank you
– Vitor Hugo
the part of the camera is leaving the photo that is taken unreadable, more if I can arrange the part that selects maybe I’ll even take the feature of the camera
– Vitor Hugo
I have the solution to both problems. But I need to separate the code and format to post the answer. I think I can get it by tomorrow 'cause I’m still working.
– Loudenvier
I noticed you insert in Schedules the image bytes and the Activity you give a select in times and take the first one that comes, returning the byte array of the image you recorded there... That won’t guarantee you’ll ever get the last picture. This seems to me a problem in the code, but I will answer only about the recording part of the image.
– Loudenvier
our thank you, saved my life, I will wait then, sorry for the abuse but if you can help me with one more problem, in my application I have a kind of event system , where I save the date of the event , I would like to make a notification , searching I found some codes but they only execute the notification at the time, my intention is to appear even with the application in the background, if you do not have the code but have at least a link q help me already I am grateful
– Vitor Hugo
really not perceived the moveToLast is inside the if was to be out, but I’m not having problems this working properly, if you notice any more notice I ask you to let me know, I’m new in android development, I am developing my TCC and all I’ve learned so far was on the internet, I haven’t had the opportunity to pay for a course but I really liked it
– Vitor Hugo
Let’s go continue this discussion in chat.
– Loudenvier
On the other problems, for example, to take the images from the camera, you can open a new question! With a specific title. About the notions in the background, same thing: open another question and try to specify well what you can not do (otherwise it can be considered too wide!). If there was something missing in the answer, or there was some doubt, comment there that I update the same in the measure of my time :-)
– Loudenvier