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

来源:互联网 发布:mac 建筑 软件 收费吗 编辑:程序博客网 时间:2024/06/05 04:13

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);    }