0

i have one activity (Main activity),in this activity i have two layout,one layout for three text box and other for button, when i click on the button layout successfully converted into bitmap and saved in SD card , but i don't know how this saved layout bitmap retrive into other activity in ONCreate Mathod. please help me i am new in android

This is my 1st activity

    Bitmap b;
    TextView textView,textView1,textView2;
    ImageView imageView;
    LinearLayout view;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.img);
        view = (LinearLayout) findViewById(R.id.ll);

        textView = (TextView) findViewById(R.id.tv1);
        textView1 = (TextView) findViewById(R.id.tv2);
        textView2 = (TextView) findViewById(R.id.tv3);
        button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            public  final String TAG =null ;

            @Override
            public void onClick(View v) {
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                b = view.getDrawingCache();
                Intent data = null;
                imageView.setImageBitmap(b);
                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 file = new File(myDir, fname);

                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                Log.i(TAG, "" + file);

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

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

            }
        });

    }
}

now how this saved image will retrive into second activity

public class Main2Activity extends AppCompatActivity {

    ImageView imageView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        ImageView mImgView = (ImageView)findViewById(R.id.img1);

    }


}
imran
  • 49
  • 1
  • 8
  • http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – Breavyn Sep 12 '15 at 07:46
  • put extra and get extra is not working in my scenario – imran Sep 12 '15 at 07:53
  • 1
    Pass the uri of the image on the sdcard, or pass the bitmap directly http://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another – Breavyn Sep 12 '15 at 07:55
  • why pass the bitmap directly??Just pass the uri in string format and in the next page parse the bitmap from the uri and displkay it in the imageview – kgandroid Sep 12 '15 at 08:29
  • 1
    you can also try another way of doing this. if you don't want to need the image for future purposes. That is converting your layout captured bitmap to string using `base64` and then sending the `base64` converted image string via `intent.putExtra()`. This will save your image writing process. – Vipul Asri Sep 12 '15 at 09:58
  • i want to use image for future, and also i am new in android. so plz review my code and modify it . thnx :) – imran Sep 12 '15 at 10:00
  • Thnx a lot :) . i have done it – imran Sep 12 '15 at 10:32

1 Answers1

2

send the path from first activity to second activity through intent extra as string. get it in second activity convert it into file by

File imgFilePath = new  File(imgStringPath);

and then convert it to bitmap and set it to the imageview like this

Bitmap myBitmap = BitmapFactory.decodeFile(imgFilePath.getAbsolutePath());
ImageView mImgView = (ImageView)findViewById(R.id.img1);
profilePic3.setImageBitmap(myBitmap);
Jolson Da Costa
  • 1,095
  • 1
  • 12
  • 31