删除手机图片后保持图库不会出现黑图的方法。
来源:互联网 发布:星际争霸2机枪兵数据 编辑:程序博客网 时间:2024/05/16 00:32
最近写了个手机相册的应用,闲的无聊想增加些功能,本来以为删除应该很easy的,结果显示会出现问题。即虽然删除了图片文件但是图库中还是会出现那张图的位置只是是以黑色取代。于是我就很苦恼的追寻原因。我觉得是不是要通知contentprovide之前的图片已被删除。(因为我的图片是根据contentprovide得到的。)甚至觉得要不要通过contentResolver来删除才行。。。
后来我通过查看Gallery的源代码发现它的删除操作是通过SQLite数据库进行操作的,而且图片存储应该是id,和byte【】数组存储。也就解释了为啥删除源文件后图片仍然会显示了。
public class PhotoAppWidgetProvider extends AppWidgetProvider { private static final String TAG = "PhotoAppWidgetProvider"; private static final boolean LOGD = true; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Update each requested appWidgetId with its unique photo PhotoDatabaseHelper helper = new PhotoDatabaseHelper(context); for (int appWidgetId : appWidgetIds) { int[] specificAppWidget = new int[] { appWidgetId }; RemoteViews views = buildUpdate(context, appWidgetId, helper); if (LOGD) { Log.d(TAG, "sending out views=" + views + " for id=" + appWidgetId); } appWidgetManager.updateAppWidget(specificAppWidget, views); } helper.close(); } @Override public void onDeleted(Context context, int[] appWidgetIds) { // Clean deleted photos out of our database PhotoDatabaseHelper helper = new PhotoDatabaseHelper(context); for (int appWidgetId : appWidgetIds) { helper.deletePhoto(appWidgetId); } helper.close(); } /** * Load photo for given widget and build {@link RemoteViews} for it. */ static RemoteViews buildUpdate(Context context, int appWidgetId, PhotoDatabaseHelper helper) { RemoteViews views = null; Bitmap bitmap = helper.getPhoto(appWidgetId); if (bitmap != null) { views = new RemoteViews(context.getPackageName(), Res.layout.photo_frame); views.setImageViewBitmap(Res.id.photo, bitmap); } return views; } static class PhotoDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "launcher.db"; private static final int DATABASE_VERSION = 2; static final String TABLE_PHOTOS = "photos"; static final String FIELD_APPWIDGET_ID = "appWidgetId"; static final String FIELD_PHOTO_BLOB = "photoBlob"; PhotoDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_PHOTOS + " (" + FIELD_APPWIDGET_ID + " INTEGER PRIMARY KEY," + FIELD_PHOTO_BLOB + " BLOB" + ");"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int version = oldVersion; if (version != DATABASE_VERSION) { Log.w(TAG, "Destroying all old data."); db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTOS); onCreate(db); } } /** * Store the given bitmap in this database for the given appWidgetId. */ public boolean setPhoto(int appWidgetId, Bitmap bitmap) { boolean success = false; try { // Try go guesstimate how much space the icon will take when // serialized to avoid unnecessary allocations/copies during // the write. int size = bitmap.getWidth() * bitmap.getHeight() * 4; ByteArrayOutputStream out = new ByteArrayOutputStream(size); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); ContentValues values = new ContentValues(); values.put(PhotoDatabaseHelper.FIELD_APPWIDGET_ID, appWidgetId); values.put(PhotoDatabaseHelper.FIELD_PHOTO_BLOB, out.toByteArray()); SQLiteDatabase db = getWritableDatabase(); db.insertOrThrow(PhotoDatabaseHelper.TABLE_PHOTOS, null, values); success = true; } catch (SQLiteException e) { Log.e(TAG, "Could not open database", e); } catch (IOException e) { Log.e(TAG, "Could not serialize photo", e); } if (LOGD) { Log.d(TAG, "setPhoto success=" + success); } return success; } static final String[] PHOTOS_PROJECTION = { FIELD_PHOTO_BLOB, }; static final int INDEX_PHOTO_BLOB = 0; /** * Inflate and return a bitmap for the given appWidgetId. */ public Bitmap getPhoto(int appWidgetId) { Cursor c = null; Bitmap bitmap = null; try { SQLiteDatabase db = getReadableDatabase(); String[] selectionArgs = { String.valueOf(appWidgetId) }; c = db.query(TABLE_PHOTOS, PHOTOS_PROJECTION, FIELD_APPWIDGET_ID + "=?", selectionArgs, null, null, null, null); if (c != null && LOGD) { Log.d(TAG, "getPhoto query count=" + c.getCount()); } if (c != null && c.moveToFirst()) { byte[] data = c.getBlob(INDEX_PHOTO_BLOB); if (data != null) { bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } } } catch (SQLiteException e) { Log.e(TAG, "Could not load photo from database", e); } finally { if (c != null) { c.close(); } } return bitmap; } /** * Remove any bitmap associated with the given appWidgetId. */ public void deletePhoto(int appWidgetId) { try { SQLiteDatabase db = getWritableDatabase(); String[] selectionArgs = { String.valueOf(appWidgetId) }; db.delete(TABLE_PHOTOS, FIELD_APPWIDGET_ID + "=?", selectionArgs); } catch (SQLiteException e) { Log.e(TAG, "Could not delete photo from database", e); } } }}
再后来在网上查到可以通过发送无序广播通知系统重新扫描内存。
//发送 一个无序广播
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
但是这个在虚拟机上面可以实现,在其他几款手机也能实现。但在小米手机上不知怎么会延迟一分钟左右后才能实现。偶然看到了:Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
http://blog.csdn.net/sgz_china/article/details/24657709
这篇文章,但是他提出的方案:
使用MediaScannerConnection执行具体文件或文件夹进行扫描。我尝试了。并没有解决,包括虚拟机上都不行。各位看到这篇文章的朋友如果解决了这个问题麻烦相告。
新手的进击之路!与君共勉。
- 删除手机图片后保持图库不会出现黑图的方法。
- 文件管理添加删除图片,视频等多媒体文件时,图库不会即时更新的问题
- [FAQ11908][FSA]进入图库编辑图片保存后,去掉原有图片被覆盖的方法
- android 删除手机中图片的方法
- Android4.4 ContentResolver查询图片无效 及 图库删除 增加图片后,ContentResolver不更新的问题解决
- 解决华为手机获取不到图库图片的路径问题
- 华为荣耀X1相机或图库图标被删除后的恢复方法(不需要恢复出厂设置)!
- Android从系统图库中读取选择后的图片
- 证明:在RB-DELETE中,如果y是红色的,则当y被删除后,红黑性质得以保持
- ADB传输图片到手机后如何在图库里显示?
- 添加.nomedia文件之后图片依然出现在图库中的解决方法小米手机
- Android调用手机图库选择图片并调用手机的图片裁剪工具
- 上传压缩后的图片并且保持100k不失真的方法
- 解决Android4.4及以上系统获取系统图片失败的问题,例如华为手机,调系统图库的时候就出现异常
- Android如何将图片存入到系统图库的方法
- Android删除图片并同步图库
- 删除hive内部表,不会删除数据的方法探索
- mac 出现黑框,出现声音提示的方法
- SQL Server事务处理
- hdu-2034-人见人爱A-B
- [zoj3314]CAPTCHA炒鸡大模拟
- Html5开发小游戏看你有多色
- 你应该了解的 一些web缓存相关的概念.
- 删除手机图片后保持图库不会出现黑图的方法。
- ASP.NET获取客户端IP及MAC地址
- MyEclipse 8.5安装Aptana
- android 屏幕适配
- int main(int argc,char* argv[])详解
- POJ--1087--A Plug for UNIX【Dinic】网络最大流
- Linux内核中常用String库函数实现
- 牛人也得看的15个CSS常识(转)
- Codeforces Round #260 (Div. 1)B题(字典树+DFS+博弈)