2
I have the following error in my project, I select an image of the gallery turn it into byte register in the database, but at the moment of loading it to imageView it does not appear and not the application error, I’m not touching the part of the photo taking code but if someone has some better code because that the image loses quality, I appreciate the help.
public class Main extends AppCompatActivity {
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
private Button btnCamera;
private Button btnSave;
private ImageView ivImage;
private String Chave;
banco db = new banco(this);
SQLiteDatabase banco;
String local;
byte[] imagem;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivImage = (ImageView) findViewById(R.id.ivImage);
btnSelect = (Button) findViewById(R.id.btnSelect);
btnCamera = (Button) findViewById(R.id.btnCamera);
btnSave = (Button) findViewById(R.id.btnSalvar);
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();
}
});
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
banco = db.getReadableDatabase();
banco.execSQL("insert into tabela (imagem) values('"+imagem+"')");
Toast.makeText(Main.this, imagem+" Imagem salva!", Toast.LENGTH_SHORT).show();
banco.close();
}
});
carregar();
}
@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;
}
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@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 onCaptureImageResult(Intent data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
local = destination.getName();
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();
}
ivImage.setImageBitmap(bitmap);
}
@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();
}
}
ivImage.setImageBitmap(bitmap);
Bitmap bitmap2 = ((BitmapDrawable)ivImage.getDrawable()).getBitmap();
ByteArrayOutputStream saida = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.PNG,100,saida);
imagem = saida.toByteArray();
Toast.makeText(Main.this, imagem.toString(), Toast.LENGTH_SHORT).show();
}
public void carregar() {
banco = db.getReadableDatabase();
Cursor cur = banco.rawQuery("select * from tabela", null);
cur.moveToFirst();
if(cur.isAfterLast()== false) {
cur.moveToLast();
byte[] image = cur.getBlob(cur.getColumnIndex("imagem"));
if (image != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
ivImage.setImageBitmap(bmp);
}
}
}
}
Did you confirm that
byte[] image
in the methodcarregar()
is not null?– ramaral