I have an sqlite database which is written to from a service running on windows(C++). I am now trying to read from this same sqlite database which contains some blob data. I have some code as follows:

String tileQuery = "SELECT * FROM '" + layerName + "' WHERE zoom_level=?";
Cursor tileCursor = database.rawQuery(tileQuery, new String[] {zoom_level});
if( tileCursor.moveToFirst() )
{
    while( !tileCursor.isAfterLast() )
    {
        int tileRow = tileCursor.getInt(tileCursor.getColumnIndex("tile_row"));
        int tileColumn = tileCursor.getInt(tileCursor.getColumnIndex("tile_column"));
        byte[] tileData = tileCursor.getBlob(tileCursor.getColumnIndex("tile_data"));

        //Write tile to file
        String fileName = layerName + "_" + zoom_level + "_" + tileRow + "_" + tileColumn + ".jpeg";

        try {
            /*
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName));
            bos.write(tileData);
            bos.flush();
            bos.close();
            */
            ByteBuffer bb = ByteBuffer.wrap(tileData);
            bb.order(ByteOrder.LITTLE_ENDIAN);
            FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + TILE_STORAGE_PATH + "/" + fileName);
            byte[] toWrite = new byte[bb.remaining()];
            bb.get(toWrite, 0 , toWrite.length);
            fos.write(toWrite);
            fos.close();
        } catch (Exception e) {
               e.printStackTrace();
        }

        tileCursor.moveToNext();
    }
}

As shown, I am attempting to write the blobs to disk as jpeg images. No matter what I do, the images appear to be corrupt, as in I cannot view them on any image viewer within android. The same images can be written to file on windows and viewed correctly, which made me think that it was an endianess issue(due to the fact that the blob was written to the database via a service running on windows). I have tried changing the byte order and writing to disk again, but I get the same result. Could anyone suggest what I might be doing wrong/missing. Any help is greatly appreciated.

有帮助吗?

解决方案

To make this work there are a few different steps. Assuming your database connection is working and those are the correct columns you are looking in with your Cursor

(1) Convert the blob to a Bitmap. You can use the blob you get back, assuming you actually downloaded and stored it to your local database, as the byte[] you will decode.

Bitmap bm = BitmapFactory.decodeByteArray(tileData, 0 ,tileData.length);

(2) Create a new file in the approprite directory and write to that file. You can do that with something like the code below. The idea is to get the local directory

private void storeBitmap(Bitmap myBitmap){
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/your_directory_name");

    String fname = "your_file_name.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
        try {
            FileOutputStream out = new FileOutputStream(file);
            myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
           e.printStackTrace();
        }
    }

If you want to add the images to gallery or you just want a different (and potentially easier) way to add the file, look into using MediaScanner which will add the files as though you took the picture with your camers

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top