• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Image from Assets folder stored in SQLite

ondrovic

Newbie
Once again I need some help, LOL. I want to store the location a series of images from the assets folder in my current android app and insert the location into a SQLite database and then display the image in an ImageView.

I have search google without any luck and really could use some help.

Thanks

Chris
 
not sure I am on the right track. I did find a example but it seems the activity uses the camera to take a picture and then store the image. Really could use some advice.
Here is the code to pull the image once stored in the database. Just need to figure out how to get the image from .png file stored in the assets folder to the byte data.

Code:
byte[] bytes = c.getBlob(c.getColumnIndex("picture"));
            if (bytes != null) {
                beer.picture = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            }

and

Code:
if (beer.getPicture() != null) {
                int width = beer.getPicture().getWidth();
                int height = beer.getPicture().getHeight();
                
                float scaleWidth = 132.0f / beer.getPicture().getWidth();
                float scaleHeight = 132.0f / beer.getPicture().getHeight();
                
                android.graphics.Matrix matrix = new android.graphics.Matrix();
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap resizedBitmap = Bitmap.createBitmap(beer.getPicture(), 0, 0, width, height, matrix, true);
                vPicture.setImageBitmap(resizedBitmap);
            }
 
Ok so I am getting a force close and it's because of this line because it works fine with out the last variable and code. Can anyone give me some pointers? I noticed that when I added in the R.drawable.beer1 that the database itself fails to be created but I can't for the life of me figure out the correct way to get the drawable resource stored in the database and then called later.

Here is my logcat log file
Code:
03-06 22:01:29.677: E/AndroidRuntime(27300): FATAL EXCEPTION: main
03-06 22:01:29.677: E/AndroidRuntime(27300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ondrovic.boombozzpassport/com.ondrovic.boombozzpassport.MainActivity}: java.lang.NullPointerException
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.os.Looper.loop(Looper.java:123)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at java.lang.reflect.Method.invoke(Method.java:507)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at dalvik.system.NativeStart.main(Native Method)
03-06 22:01:29.677: E/AndroidRuntime(27300): Caused by: java.lang.NullPointerException
03-06 22:01:29.677: E/AndroidRuntime(27300):     at com.ondrovic.boombozzpassport.Database.addBeer(Database.java:87)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at com.ondrovic.boombozzpassport.Database.onCreate(Database.java:49)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at com.ondrovic.boombozzpassport.MainActivity.onCreate(MainActivity.java:36)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 22:01:29.677: E/AndroidRuntime(27300):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-06 22:01:29.677: E/AndroidRuntime(27300):     ... 11 more
here is my function to add a new item to the database
Code:
private void addBeer(SQLiteDatabase db, String name, String brewer, String type, String bdesc, String fdesc, int abv, int rate, int icon) {
        final ContentValues cv = new ContentValues();
        cv.put(COL_NAME, name);
        cv.put(COL_BREWER, brewer);
        cv.put(COL_TYPE, type);
        cv.put(COL_BDESC, bdesc);
        cv.put(COL_FDESC, fdesc);
        cv.put(COL_ABV, abv);
        cv.put(COL_RATE, rate);
        
        final Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), icon);
        writeBitmap(cv, COL_PIC, bitmap);

        
        db.insert(TABLE_BEERS, null, cv);
    }
here is the code to write the image to the database
Code:
static void writeBitmap(ContentValues cv, String name, Bitmap image) {
        if (image != null) {
            try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            cv.put(name, out.toByteArray());
        
            } catch (IOException e) {
                
            }
        }
and here is where I add the info to pass to the functions

Code:
addBeer(db, "NAME 1", "BREWER 1", "TYPE 1", "BDESC 1", "FDESC 1", 0, 0, R.drawable.beer1);
I have a jpg image called beer1 in my drawable folder but cant seem to figure out why it's give me a force close

Thanks
 
Also here is my table creation
Code:
db.execSQL("CREATE TABLE beers ("
                + "_id INTEGER PRIMARY KEY, "
                + "name TEXT, "
                + "brewer TEXT, "
                + "abv REAL, "
                + "rating REAL, "
                + "breifdescription TEXT, "
                + "fulldescription TEXT, "
                + "type TEXT, "
                + "picture BLOB);");
 
Back
Top Bottom