Java DOM 设置 XML Standalone

    public void save2file() {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(mDocument);

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");

            PrintWriter pw = new PrintWriter(new FileOutputStream(mFileName));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

输出:

 <?xml version="1.0" encoding="utf-8" standalone="no"?>

这里的 transformer.setOutputProperty(OutputKeys.STANDALONE, “yes”);居然不起效!

GOOGLE了一通,加了一行mDocument.setXmlStandalone(true);,变成了下面的代码:

    public void save2file() {
            mDocument.setXmlStandalone(true);
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(mDocument);

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");

            PrintWriter pw = new PrintWriter(new FileOutputStream(mFileName));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

输出:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

输出结果是正确了,但还没空搞清楚为啥( ⊙ o ⊙ )

MySql 密码重置

如果不小心忘了Mysql的密码怎么办?很简单,按下面的步骤就可以重置你的密码

OS: ubuntu 12.04

  • 停止MySql服务

    service mysql stop
    
  • 以安全模式启动MySql

    mysqld_safe --skip-grant-tables &
    
  • 登入Mysql并修改密码

    mysql -uroot
    
    mysql> update mysql.user set password=PASSWORD('1234567890') where User='root';
    
    mysql> quit;
    
  • 退出安全模式

    killall mysqld
    
  • 重启Mysql服务

    service mysql start
    

PHP event-based XML parser

被解析的XML文件 book.xml

            Rasmus Lerdorf
            Kevin Tatroe
            Peter MacIntyre
        
        1-5692-610-2
        A great book!
            Rasmus Lerdorf
        
        1-56592-769-9
        It really does fit in your pocket

Parser XML

<html>
    <head>My Library</head>

    <body>
        <?php
        error_reporting(0);

        class BookList {

            const FIELD_TYPE_SINGLE = 1;
            const FIELD_TYPE_ARRAY = 2;
            const FIELD_TYPE_CONTAINER = 3;

            var $parser;
            var $record;
            var $currentField = '';
            var $fieldType;
            var $endsRecord;
            var $records;

            function __construct($filename) {
                $this->parser = xml_parser_create();
                xml_set_object($this->parser, $this);
                xml_set_element_handler($this->parser, "elementStarted", "elementEnded");
                xml_set_character_data_handler($this->parser, "handleCdate");

                $this->fieldType = array(
                    'title' => self::FIELD_TYPE_SINGLE,
                    'author' => self::FIELD_TYPE_ARRAY,
                    'isbn' => self::FIELD_TYPE_SINGLE,
                    'comment' => self::FIELD_TYPE_SINGLE,
                );

                $this->endsRecord = array('book' => true);

                $xml = join('', file($filename));
                xml_parse($this->parser, $xml);

                xml_parser_free($this->parser);
            }

            function elementStarted($parser, $element, &$attributes) {
                $element = strtolower($element);

                if ($this->fieldType[$element] != 0) {
                    $this->currentField = $element;
                } else {
                    $this->currentField = '';
                }
            }

            function elementEnded($parser, $element) {
                $element = strtolower($element);

                if ($this->endsRecord[$element]) {
                    $this->records[] = $this->record;
                    $this->record = array();
                }

                $this->currentField = '';
            }

            function handleCdate($parser, $text) {
                if ($this->fieldType[$this->currentField] == self::FIELD_TYPE_SINGLE) {
                    $this->record[$this->currentField] .= $text;
                } else if ($this->fieldType[$this->currentField] == self::FIELD_TYPE_ARRAY) {
                    $this->record[$this->currentField][] = $text;
                }
            }

            function showMenu() {
                echo "<table>\n";

                foreach ($this->records as $book) {
                    echo "<tr>";
                    echo "<th><a href=\"{$_SERVER['PHP_SELF']}?isbn={$book['isbn']}\">";
                    echo "{$book['title']}</a></th>";
                    echo "<td>" . join(', ', $book['author']) . "</td>\n";
                    echo "</tr>\n";
                }
                echo "</table>\n";
            }

            function showBook($isbn) {
                foreach ($this->records as $book) {
                    if ($book['isbn'] != $isbn) {
                        continue;
                    }
                    echo "<p><b>{$book['title']}</b> by " . join(', ', $book['author']) . "<br />";
                    echo "ISBN: {$book['isbn']}<br />";
                    echo "Commit: {$book['comment']}</p>\n";
                }
                echo "<p>Back to the <a href=\"{$_SERVER['PHP_SELF']}\">list of books</a>. </p>";
            }

        }

        $library = new BookList("books.xml");

        if (isset($_GET['isbn'])) {
            $library->showBook($_GET['isbn']);
        } else {
            $library->showMenu();
        }
        ?>

    </body>
</html>

ref: 《Programming PHP》 p.280

Android 对大图的处理

Scale big bitmap and set Keyguard Wallpaper

private void processBigBitmapAndSetKeyguardWallpaper(Uri _uri) {

            InputStream inStream = null; //receive from uri
            InputStream reOpenStream = null; //Raise efficiency and avoid OOM
            InputStream outStream = null; //send to keyguard
            Uri uri = _uri;

            try {
                inStream = mContext.getContentResolver().openInputStream(uri);

                //API > 13
                WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                Point point = new Point();
                display.getRealSize(point);

                BitmapFactory.Options opts = new BitmapFactory.Options();

                // For test the bitmap, just get the bitmap size, will not allocate memory for the big Bitmap
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(inStream, null, opts);
                
                // Then scale the big bitmap
                int scale1 = Math.max(opts.outHeight, opts.outWidth)
                                    / Math.max(point.x, point.y);
                int scale2 = Math.min(opts.outHeight, opts.outWidth)
                                    / Math.min(point.x, point.y);

                opts.inSampleSize = Math.max(scale1, scale2);
                opts.inJustDecodeBounds = false;

                reOpenStream = mContext.getContentResolver().openInputStream(uri);

                Bitmap scaledBitmap = BitmapFactory.decodeStream(reOpenStream, null, opts);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                scaledBitmap.recycle();
                outStream = new ByteArrayInputStream(baos.toByteArray());
                WallpaperManager wallpaperManager = WallpaperManager
                        .getInstance(mContext);
                wallpaperManager.setKeyguardStream(outStream);

            } catch (FileNotFoundException e) {
                Log.d(TAG, uri.toString() + " not found" + e.toString());
            } catch (IOException e) {
                Log.d(TAG, "set keyguard error" + e.toString());
            } finally {
                try {
                    if (null != inStream)
                        inStream.close();

                    if(null != outStream)
                        outStream.close();

                    if(null != reOpenStream)
                        reOpenStream.close();

                } catch (IOException e) {
                    Log.d(TAG, "set keyguard error" + e.toString());
                }
            }
            return true;
        }

ref: http://blog.hesheyou.me/post/crop-large-image-with-android

解决 KitKat 对SD卡的读写限制

前提是你有Android源码或者设备已经ROOT,如果不考虑系统安全或者CTS等条件,可以选择修改系统文件来完全开放KitKat对SD卡的读写权限,让第三方APP以KitKat之前版本相同的方式操作SD卡。

修改文件:/system/etc/permissions/platform.xml

原始内容:

    <permission name="android.permission.WRITE_EXTERNAL_STORAGE" >             
        <group gid="sdcard_r" />          
        <group gid="sdcard_rw" />                                            
    </permission> 

修改后:

    <permission name="android.permission.WRITE_EXTERNAL_STORAGE" >             
        <group gid="sdcard_r" />          
        <group gid="sdcard_rw" />                                            
        <group gid="media_rw" />
    </permission>

修改之后第三方APP只需要跟以前一样申请<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />来读写整个SD卡了。

ref: http://developer.huawei.com/devunion/ui/newsDetail.html?news=/devunion/news/n_00033.html