In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In order to solve the problem of how to access pictures in Android database, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.
1. Database creation and access methods:
When the database is created, the data type of the picture field is stored as a BLOB database insert operation
Public void onCreate (SQLiteDatabase db) {String sql = "create table" + TB_NAME + "(" + ID + "integer primary key," + IMAGE + "BLOB)"; db.execSQL (sql);}
Store the picture in one-byte form for database read operation
Public long insert (byte [] img) {SQLiteDatabase db = getWritableDatabase (); ContentValues cv = new ContentValues (); cv.put (IMAGE, img); long result = db.insert (TB_NAME, null, cv); return result;}
The resulting picture is of Bitmap type. Click on the position of the picture in ListView, GridView, etc., parameter position, and you can write code 2 as needed.
Public Bitmap getBmp (int position) {SQLiteDatabase db = getReadableDatabase (); Cursor cursor = select (TB_NAME); cursor.moveToPosition (position); byte [] in = cursor.getBlob (cursor.getColumnIndex (IMAGE)); Bitmap bmpout = BitmapFactory.decodeByteArray (in, 0, in.length); return bmpout;}
2. Convert pictures to byte [] / / Parameter id to image resources (R.drawable.img)
Public byte [] img (int id) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); Bitmap bitmap = ((BitmapDrawable) getResources (). GetDrawable (id)). GetBitmap (); bitmap.compress (Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray () } this is the answer to the question about how to access pictures in the Android database. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.