Android图片下载到本地,系统图库不显示

可能大家都知道我们下载图片到Android手机的时候,然后调用系统图库打开图片,提示“找不到指定项”。

那是因为我们插入的图片还没有更新的缘故,所以只要将图片插入系统图库,之后发条广播就ok了。

/**
     * 图片插入到系统相册,解决系统图库不能打开图片的问题
     */
    public static void insertImageToSystemGallery(Context context, String filePath, Bitmap bitmap){
        MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "", "");
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(new File(filePath));
        intent.setData(uri);
        context.sendBroadcast(intent);
    }

附上获取图片路径的方法:

//获取图片的绝对路径
private  String getFilePathByContentResolver(Context context, Uri uri) {
		if (null == uri) {
			return null;
		}
        Cursor c = context.getContentResolver().query(uri, null, null, null, null);
        String filePath  = null;
        if (null == c) {
            throw new IllegalArgumentException(
                    "Query on " + uri + " returns null result.");
        }
        try {
            if ((c.getCount() != 1) || !c.moveToFirst()) {
            } else {
            	filePath = c.getString(
            			c.getColumnIndexOrThrow(MediaColumns.DATA));
            }
        } finally {
            c.close();
        }
        return filePath;
    }

  

posted @ 2016-11-02 16:53  ha_cjy  阅读(1893)  评论(0编辑  收藏  举报