Reputation: 59
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);
}
}
Upvotes: 0
Views: 101
Reputation: 1105
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);
Upvotes: 2