Android界面调试的基本方法---bitmap显示出来

Android界面调试的基本方法—bitmap显示出来

大家在代码中是不是经常会遇到bitmap呢?你们在分析、解决问题的时候有没有想过看看这个bitmap到底是什么,对不对劲?
(Drawable之类的也是可以转化成bitmap的)
下面提供一种方法就是将:bitmap转换成jpeg图片,并且在gallery中显示出来的代码。

这个例子很简单,希望对大家在分析问题的时候有帮助 =>

    static int mFilePathNum = 0;
    public static void saveBitmapImage(Bitmap source, Context context) {//只要是Context对象都行
        // We should store image data earlier than insert it to ContentProvider,
        // otherwise we may not be able to generate thumbnail in time.
        String directory = "/storage/sdcard0/Pictures";//存储路径
        String filename = "123_" + mFilePathNum + ".jpg";//名字,随便你定义
        mFilePathNum ++;

        OutputStream outputStream = null;
        String filePath = directory + "/" + filename;
        try {
            File dir = new File(directory);
            if (!dir.exists()) dir.mkdirs();
            File file = new File(directory, filename);

            outputStream = new FileOutputStream(file);
            source.compress(Bitmap.CompressFormat.JPEG, 75, outputStream);

        } catch (FileNotFoundException ex) {
            Log.v(TAG, "yunhen FileNotFoundException :" + ex);
            return ;
        } catch (IOException ex) {
            Log.v(TAG, "yunhen IOException :" + ex);
            return ;
        } finally {
            if (outputStream!= null){
                try {
                    outputStream.close();
                } catch (Throwable t) {
                    // do nothing
                }
            }
        }

        // Read back the compressed file size.
        long size = new File(directory, filename).length();

        ContentValues values = new ContentValues(9);
        // That filename is what will be handed to Gmail when a user shares a
        // photo. Gmail gets the name of the picture attachment from the
        // "DISPLAY_NAME" field.

        values.put(MediaStore.Images.ImageColumns.DATA, filePath);
        values.put(MediaStore.Images.ImageColumns.TITLE, filename);

        values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATA, filePath);
        values.put(MediaStore.Images.Media.SIZE, size);

        context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Log.e("yunhen", "yunhen SaveBitmapImage filePath = " + filePath);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值