I have an Activity1 which I can take pictures with device camera and convert to bitmap to display on ImageView, compress to save on a bundle to display on seconds activity2
code activity 1
//declare
private ImageView img_1
img_1 = (ImageView) this.findViewById(R.id.img_1);
//code
img_1.buildDrawingCache();
Bitmap image = img_1.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] food = stream.toByteArray();
Bundle extras = new Bundle();
//intent img1
Intent intent = new Intent(this, activity2.class);
intent.putExtras(extras);
intent.putExtra("picture", food);
startActivity(intent);
//method take picture
//Button img1
public void img1_takePicture (View view)
{
num = 1;
contadorImg = 1;
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(
Environment.getExternalStorageDirectory(), "proyect");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "temp1.png");
Uri uriSavedImage = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(cameraIntent, 1);
}
//method onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (num == 1)
{
if (requestCode == 1 && resultCode == RESULT_OK)
{
//convert bitmap
Bitmap bMap = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() +
"/proyect/" + "temp1.png");
//show bitmap
img_1.setImageBitmap(bMap);
}
}
second Activity2
I retrieve bundle with bitmap and show on another ImageView to later save that image on Byte array [] and save on SQLite Database.
//code
//declare
private ImageView img_1_confir;
public BBDD database;
private byte[] img = null;
img_1_confir = (ImageView) findViewById(R.id.img_1_confir);
//retrieve img1, convert to byte array and save on BBDD
Bundle extras = getIntent().getExtras();
byte[] food = extras.getByteArray("picture");
Bitmap fo = BitmapFactory.decodeByteArray(food, 0, food.length);
img_1_confir.setImageBitmap(fo);
//button send
Bitmap bitmap = ((BitmapDrawable) img_1_confir.getDrawable()).getBitmap();
// convert to byte array and save on BBDD
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
img = bos.toByteArray();
//insert
database = new BBDD(this, "BBDD", null, 1);
SQLiteDatabase db = database.getWritableDatabase();
ContentValues registro = new ContentValues();
registro.put("imgone", img);
if (db.insert("database", null, registro) != -1)
{
Toast.makeText(this, "Registro insert", Toast.LENGTH_LONG)
.show();
db.close();
}
Activity Database BBDD
public class BBDD extends SQLiteOpenHelper
{
private static final String KEY_ID = "id";
String crear = "CREATE TABLE infotra(KEY_ID INTEGER PRIMARY KEY," +
"imgone blob) ";
public BBDD (Context contexto, String nombre, CursorFactory factory,
int version)
{
super(contexto, nombre, factory, version);
}
public void onCreate (SQLiteDatabase db)
{
db.execSQL(crear);
}
public void onUpgrade (SQLiteDatabase db, int versionAnt, int versionNue)
{
db.execSQL(crear);
}
NOW MY PROBLEM I have another Activity (activity3). That activity have a ListView with CustomAdapter, when I click on one position on that ListView I need get that byte array from SQLite database, convert to Bitmap and display on another ImageView.
THATS WORKS BUT… CODE ACTIVITY3
//DECLARE
private byte[] img1 =null;
ListView listView;
BBDD database;
private ImageView img_1_confir;
listView = (ListView) findViewById(R.id.listView);
img_1_confir=(ImageView)findViewById(R.id.img_1_confir);
//code
//listview clic
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick (AdapterView<?> parnet, android.view.View view,
int position, long id)
{
database = new BBDD(Activity3.this, "BBDD", null, 1);
SQLiteDatabase db2 = database.getReadableDatabase();
if (db2 != null)
{
//retrieve image1
String[] col={"imgone"};
Cursor cursor1=db2.query("database", col, null, null, null, null, null);
if(cursor1!=null){
cursor1.moveToFirst();
do{
img1=cursor1.getBlob(cursor1.getColumnIndex("imgone"));
}while(cursor1.moveToNext());
}
Bitmap b1=BitmapFactory.decodeByteArray(img1, 0, img1.length);
img_1_confir.setImageBitmap(b1);
}
MY FINAL PROBLEM
Thats Works but if i take another pic and and I keep in a new row in the database (there are two rows then), obviously another item appears on Activity3's ListView but if I clic any of the items always appears last saved image….What is the problem? Why only appears last saved?
Aucun commentaire:
Enregistrer un commentaire