2

相対的なレイアウトをビットマップ画像として保存する方法....? 実行時に相対レイアウトに画像を追加しています..次に、ビットマップ画像として保存する方法。

この概念についてはわかりません...これに対する解決策を提案してください。

ありがとうございました。

4

2 に答える 2

9

XML ファイルを作成します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/rlid"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

次に、アクティビティで次のコードを使用します。

View content = findViewById(R.id.rlid);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/" + yourimagename + ".png");
try {
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream ostream = new FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 10, ostream);
    ostream.close();
    content.invalidate();
} catch (Exception e) {
    e.printStackTrace();
} finally {
        content.setDrawingCacheEnabled(false);
}
于 2012-10-20T07:00:38.557 に答える
0

これを試してください:1つのボタンを相対的なレイアウトに配置してから、SDカードに保存されている画像に対してこのプロセスを実行します

        btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            bm = view.getDrawingCache();

            String root = Environment.getExternalStorageDirectory()
                    .toString();
            File myDir = new File(root + "/_images");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            file = new File(myDir, fname);

            Log.i(TAG, "" + file);

            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
于 2012-10-20T07:03:12.007 に答える