2012-10-20 27 views
1

如何将相对布局保存为位图图像....? 在运行时我将图像添加到相对布局..然后如何将其保存为位图图像。如何将图像保存为RelativeLayout

我对这个概念没有任何想法...请为此提出解决方案。

谢谢。

+0

拍摄快照并保存为位图图像并存储为你喜欢的图像在SD卡 –

+0

嗨SubbaReddy ...感谢你的答复.... – satish

+0

你有位图和存储图像在SD卡? –

回答

7

创建一个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); 
} 
+0

我试过这个,但是file.createNewFile()有一个错误; – MMakati

+0

@MMakati什么是错误? – Raghunandan

+0

它去抓块。 “java.io.IOException:打开失败:EACCES(权限被拒绝)” – MMakati

0

试试这个: 把一个按钮,在相对布局,然后执行此过程中的图像存储在sdcard

 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(); 
      } 
     } 
    }); 
+0

mapview ... 。这个变量是什么..? – satish

+0

异常....在bm = mapview.getDrawingCache(); line .. – satish

+0

那是不是mapview取代它与视图 –