2

I am saving total relative layout as bitmap.After saving,if i see the image,the background of the image is in black color.How do i get my original background?Please help me.

My main Activity is

  import java.io.File;
  import java.io.FileOutputStream;
  import java.util.Random;
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.Bitmap.CompressFormat;
  import android.os.Bundle;
  import android.os.Environment;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.Toast;


   public class Save extends Activity{
     Button save;
    ImageView iv;
     RelativeLayout relative;
    Bitmap bitmap;

     public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save);
save=(Button)findViewById(R.id.button1);
iv=(ImageView)findViewById(R.id.imageView1);
relative=(RelativeLayout)findViewById(R.id.relative);
save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        relative.setDrawingCacheEnabled(true);
        relative.buildDrawingCache();
         bitmap = relative.getDrawingCache();

        try {
            File rootFile=new File(Environment.getExternalStorageDirectory().toString()+"/MYPICS");
            rootFile.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-"+ n +".png";

          File  resultingfile=new File(rootFile, fname);

            if (resultingfile.exists ()) resultingfile.delete (); 
            try {
                   FileOutputStream Fout = new FileOutputStream(resultingfile);
                   bitmap.compress(CompressFormat.PNG, 100, Fout);
                   Fout.flush();
                   Fout.close();

            } catch (Exception e) {
                   e.printStackTrace();
            }
      } catch (Exception e) {
    }
  Toast.makeText(getApplicationContext(), "Image is saved", Toast.LENGTH_SHORT).show();

     }
      });

     }
      }

my xml is

<?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:id="@+id/relative"
 >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="Save" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

   </RelativeLayout>
Lakshmipathi
  • 87
  • 2
  • 9
  • Set background color for relative layout. – Shini Apr 03 '14 at 09:30
  • This could be the solution: [How to programatically take a screenshot on Android?](http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android#answer-5651242)? – Yehor Nemov Apr 03 '14 at 09:49
  • [duplicate](http://stackoverflow.com/questions/22830382/why-do-i-get-black-background-when-i-save-an-image-in-android)? – Onik Apr 03 '14 at 10:03
  • If i set layout background to "@color/abc_search_url_text_normal", it is ok.But i want the original white background it self. – Lakshmipathi Apr 03 '14 at 10:13
  • I solved my problem.Create an xml file named colours and just add #FFFFFFFF to it inside resources.and after that in your layout add android:background="@color/White" to the layout to get the same white background. – Lakshmipathi Apr 03 '14 at 11:05

0 Answers0