I have spent a week reading a lot of questions and answers, tutorials, android documentations, videos etc. but still a lot of them are complicated, highly personalized examples with complicated data structures, interfaces and highly customized adapters of how to set all images retrieved from SQLite database to a listview. I just want a very direct, simple way of setting all my images from SQLite database to show on a simple listview. Android documentation does not have a very clear and complete example on this as well. My program has no highly customized adapter, no all those fancy data structures. I am sure that my question will benefit a lot of people trying to do the same thing because trust me I have browsed through many documentations, methods, tutorials, videos on youtube, and many questions and answers out there but none have a straightforward, understandable way to simply populate all the images from an SQLite to a ListView using just one class.
What this program is supposed to do is when it starts up, and when the camera icon on the activity bar is clicked, a camera will be opened and if a user takes a photo and oks to save it the picture will be saved in the database and the photo will be shown on the main page as a thumbnail in a ListView immediately after the camera instance is gone. I have tried viewing the database using adb and the storing mechanism is right, just the populating part is not.
So what is a simple way of populating all images from SQLite database to ListView?
I have tried many fancy ways as a result spent a lot of days in the process. So I have reverted back to this simpler code example that works so far below so it won't complicate your answering effort.
Below are my codes:
MainActivity:
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.io.ByteArrayOutputStream;
public class MainActivity extends ActionBarActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
SQLiteDatabase db;
private ListView listView;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = this.openOrCreateDatabase("images.db", Context.MODE_PRIVATE, null);
db.execSQL("create table if not exists tb ( image blob)");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
//dispatchTakePictureIntent();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//Calling dispatchTakePictureIntent to start the camera activity
dispatchTakePictureIntent();
return true;
}
return super.onOptionsItemSelected(item);
}
private void dispatchTakePictureIntent() {
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();
Bitmap imageTaken = (Bitmap) extras.get("data");
ContentValues values = new ContentValues();
byte[] toValuesPut = this.getBytes(imageTaken);
values.put("image", toValuesPut);
db.insert("tb", null, values);
getImage();
db.close();
}
}
protected void getImage(){
listView = (ListView) findViewById(R.id.parentListView);
Cursor c = db.rawQuery("select * from tb", null);
//startManagingCursor(c);
if(c.moveToNext()) {
byte[] image = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
ImageView thumb1 = (ImageView) findViewById(R.id.child2);
thumb1.setImageBitmap(bmp);
}
}
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<ImageView
android:id="@+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/parentListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp" >
</ListView>
</LinearLayout>
childinlistview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/child"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
/>
</LinearLayout>
Thank you so much!
Aucun commentaire:
Enregistrer un commentaire