android 下载图片及时显示

今年刚刚毕业,开始做一个android 的项目,在点击图片浏览的时候,对于当前的图片可以下载:但是存在一个问题就是图片下载后,该项目有上传的功能,在我打开本地的相框的时候无法显示我刚刚下载的图片,必须关机再开才可以,很是郁闷,后来经查找如下方法可以实现即使的显示图片:
if (mBitmap == null)
        {
            Toast.makeText(this, "图片不存在,无法保存", Toast.LENGTH_LONG).show();
            return;
        }
File f = new File("/sdcard/DCIM/Camera");
        if (!f.exists())
        {
            f.mkdir();
        }
File f = new File("/sdcard/DCIM/Camera/" + bitName);
        if (!f.exists())
        {
            f.createNewFile();
        }

        FileOutputStream fOut = null;
        try
        {
            fOut = new FileOutputStream(f);
        }
        catch (FileNotFoundException e)
        {
            Log.d(ApplicationConstant.TAG, Log.getStackTraceString(e));
        }
        if (null == fOut)
        {
            Toast.makeText(PicDetailActivity.this, getResources().getString(R.string.title_downloadpicfailed),
                    Toast.LENGTH_LONG).show();
        }
        else
        {
            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        }

        try
        {
            fOut.flush();
            fOut.close();
            // 广播插入图片
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(f);
            intent.setData(uri);
            sendBroadcast(intent);
            Toast.makeText(PicDetailActivity.this, getResources().getString(R.string.title_downloadpicsuccess),
                    Toast.LENGTH_LONG).show();
        }
        catch (IOException e)
        {
            Toast.makeText(PicDetailActivity.this, getResources().getString(R.string.title_downloadpicfailed),
                    Toast.LENGTH_LONG).show();
            Log.d(ApplicationConstant.TAG, Log.getStackTraceString(e));
        }

你可能感兴趣的:(android)