将相对布局转换为位图,并使用它的子元素将其保存到手机库

时间:2013-04-16 06:52:04

标签: java android android-layout bitmap relativelayout

我正在尝试将RelativeLayout转换为位图,并希望将其与子元素一起保存到手机库。但是当我保存它时,只有一个图像保存在图库中,而另一个图像没有保存。

public class Result extends Activity implements android.view.View.OnClickListener{
    private MaskAdjuster mIV;
    private Bitmap mBitmap,b;
    private Bundle getValues;
    private Button save,moreApp;
    private ImageView mask;
    private BitmapDrawable d;
    private static File APP_FILE_PATH = new File("/sdcard/rISHABH/");
    private int x,y;
    private int []epx=null;
    private int []epy=null;
    private int []lpx=null;
    private int []lpy=null;
    private float eyedistance;
    private int count;
    public String filename;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mIV=new MaskAdjuster(this);
    setContentView(R.layout.result);
    initialize();
    b=getIntent().getParcelableExtra("bitmap");
    mBitmap=b.copy(Bitmap.Config.RGB_565, true);
    getValues=getIntent().getBundleExtra("bundle");
    epx=getValues.getIntArray("eyelocationX");
    epy=getValues.getIntArray("eyelocationY");
    lpx=getValues.getIntArray("liplocationX");
    lpy=getValues.getIntArray("liplocationY");


    eyedistance=getValues.getFloat("eyedistance");
    count=getValues.getInt("count");
    mIV=(MaskAdjuster)findViewById(R.id.iv_resultface);
    mIV.setImageBitmap(mBitmap);
    mIV.setDisplayPoints(epx, epy,lpx,lpy, count * 2, eyedistance);
    mIV.invalidate();
    mask.setImageBitmap(MaskAdjuster.getFace());
    save.setOnClickListener(this);
    moreApp.setOnClickListener(this);
    }


    private void initialize() {
    save=(Button)findViewById(R.id.btn_save);
    moreApp=(Button)findViewById(R.id.btn_more_app);
    mask=(ImageView)findViewById(R.id.iv_mask);
    }


    @Override
    protected void onPause() {
    // TODO Auto-generated method stub

    super.onPause();

    b.recycle();
    finish();

    }


    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_save:

    try{
    saveandforward();


    Toast.makeText (getApplicationContext(), 
    "Image has been saved to Gallery(SD card) by name : " + APP_FILE_PATH +            filename, Toast.LENGTH_LONG).show ();

    }catch (Exception e) {
    e.printStackTrace();
    }

    break;
    case R.id.btn_more_app:

    break;

    default:
    break;
    }
    }



    void saveandforward(){
    Bitmap bm=null;
    try {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
    filename = sdf.format(cal.getTime());

    final FileOutputStream out = new FileOutputStream(new File      (APP_FILE_PATH     +filename+".png"));
    try{
    View vs = (View)findViewById(R.id.Frame_layout);
    bm = loadBitmapFromView(vs);

    }
    catch(Exception e)
    {
    Log.e("meesge",e.getMessage()); 

    }
    bm.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.flush();
    out.close();

    }catch (Exception e) {
    e.printStackTrace();
    }

    }
    public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(320,480, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;

         }
    }

xml在这里

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<FrameLayout
android:id="@+id/Frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<com.shagun.agemyface.MaskAdjuster
android:id="@+id/iv_resultface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv_mask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

/>
</FrameLayout>
<Button 
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/save"/>

<Button 
android:id="@+id/btn_more_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="More App"/>




</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

我想你想创建布局的屏幕截图 请尝试使用它。

RelativeLayout layout = (RelativeLayout)findViewById(R.id.x);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);

答案 1 :(得分:0)

问题是您实际上是在代码中保存FrameLayout。这是您的问题所在:

View vs = (View)findViewById(R.id.Frame_layout);

为您的RelativeLayout添加一些ID,并将行更改为:

View vs = findViewById(R.id.Id_Of_Relative_Layout);

答案 2 :(得分:0)

    RelativeLayout rlMain = (RelativeLayout)findViewById(R.id.abc);

    rlMain.setDrawingCacheEnabled(true);
    Bitmap bitmap = rlMain.getDrawingCache();


    String file = getFilename();

    try {

        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
        ostream.close();
        rlMain.invalidate();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        rlMain.setDrawingCacheEnabled(false);
    }



    private String getFilename() {
    File file = new File(IMAGE_PATH);
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/"
            + System.currentTimeMillis() + ".jpg");
    return uriSting;
}