How to store image in SQLite database from gallery

前端 未结 3 1458
半阙折子戏
半阙折子戏 2021-01-01 07:40

i have tried this code to upload an image from gallery to sqllite database in my application but when my application tries to open gallery it gives FORCE TO CLOSE Error and

相关标签:
3条回答
  • 2021-01-01 08:06

    Using BLOB data type we can store an image in the SQLite database. The data that actually gets stored in the database is the bytes that make up an image or a file.

    As for obtaining pic from gallery, look here, it seems, you have forgotten "super" call.

    0 讨论(0)
  • 2021-01-01 08:11
    db.execSQL("CREATE TABLE images (" 
        + "_id INTEGER PRIMARY KEY AUTOINCREMENT," 
        + "data BLOB," 
        + "hash BLOB UNIQUE" 
        + ");"); 
    

    To convert the image to a BLOB, use the following code: (Note that we're compressing the image here)

     private byte[] getBitmapAsByteArray(Bitmap bitmap)
     { 
       ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
       // Middle parameter is quality, but since PNG is lossless, it doesn't matter 
       bitmap.compress(CompressFormat.PNG, 0, outputStream); 
       return outputStream.toByteArray(); 
    } 
    
    0 讨论(0)
  • 2021-01-01 08:18

    Import form device by button click

    Intent sdintent = new Intent(Intent.ACTION_PICK);
    sdintent.setType("image/*");
    startActivityForResult(sdintent, SD_REQUEST);
    

    Get image form sd card

    if (requestCode == SD_REQUEST) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
    Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
    cursor.moveToFirst();
    
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();
    
    Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    
    testimage.setImageBitmap(yourSelectedImage);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byteArray = stream.toByteArray();
    }
    

    Save Image

    DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this);
    
           dbHelper.open();
           dbHelper.createUserProfiles( byteArray);
           dbHelper.close();
    

    NOw in DatabaseAdapter.java

    Define

      public static final String U_PIC = "picture";
    

    Then

    private long createUserTableContentValues(long id,byte[] byteImage) {
            ContentValues values = new ContentValues();
            values.put(ID, id);
            values.put(U_PIC, byteImage);
    return database.insert(IMAGE_INSERT, null, values);
    }
    

    I think this might be help you.....

    Other resources: http://www.helloandroid.com/tutorials/store-imagesfiles-database

    http://www.coderanch.com/t/507054/Android/Mobile/Storing-image-database

    http://hi.baidu.com/_java/blog/item/971e142a13afe6305243c12f.html

    http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

    0 讨论(0)
提交回复
热议问题