Android中把View转换成bitmap,再缓存到sd卡

首先把当前页面转换成bitmap

<strong> </strong> View view = viewRl.getRootView();
                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = view.getDrawingCache();

viewRl为布局的根id

 this.viewRl= (RelativeLayout) findViewById(R.id.viewRl);

然后把bitmap缓存到sd中

 public static String saveCroppedImageSD(Bitmap bmp,Context context) {
        //获取应用包名
        PackageManager pm = context.getPackageManager();
        String appName = context.getApplicationInfo().loadLabel(pm).toString();

        String sdPath= Environment.getExternalStorageDirectory().getPath();
        File file = new File(sdPath+"/"+appName);
        if (!file.exists())
            file.mkdir();


        // /sdcard/包名/temp_cropped.jpg
        String newFilePath = sdPath+"/"+appName + "/"  + "temp_cropped.jpg" ;
        file = new File(newFilePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();

            if(file.exists()){
                Log.v("保存bitmap为缓存文件111", "成功保存bitmap:"+file.getPath());
            }
            return newFilePath;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
当然也可以进行图片的删除

 public static  void deleteCacheImage(Context context){
        //获取应用包名
        PackageManager pm = context.getPackageManager();
        String appName = context.getApplicationInfo().loadLabel(pm).toString();
        String sdPath= Environment.getExternalStorageDirectory().getPath();

        File file = new File(sdPath+"/"+appName+"/temp_cropped.jpg");
        if(file.exists()){
            file.delete();
        }
    }<pre name="code" class="html">

 除了可以保存到sd中,也可以创建缓存,放在缓存中 

 public static String saveCroppedImageCache(Bitmap bmp, Context context) {
        try {
            //获取缓存地址
            File outputDir = context.getCacheDir();
            //创建缓存文件
            File outputFile = File.createTempFile(System.currentTimeMillis() + "", ".jpg", outputDir);

            FileOutputStream fos = new FileOutputStream(outputFile);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
            //保存位置
//            String imgUrl= context.getCacheDir()+"/"+ outputFile.getName();
            if(outputFile.exists()){
                Log.e("保存bitmap为缓存文件111", "成功保存bitmap:"+outputFile.getPath());
            }
            return outputFile.getPath();

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("保存bitmap为缓存文件", "失败保存bitmap");
        }
        return null;
    }


参考的例子链接http://www.daxueit.com/article/5182.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值