我有一个活动(主要活动),在这个活动中我有两个布局,一个布局为三个文本框,另一个为按钮,当我点击按钮布局成功转换为位图并保存在SD卡,但我不知道这个保存的布局位图如何在ONCreate Mathod中重新进入其他活动。 请帮帮我,我是android的新手
这是我的第一个活动
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();
}
}
});
}
}
现在这个保存的图像将如何重新进入第二个活动
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);
}
}
答案 0 :(得分:2)
通过intent extra as string将第一个活动的路径发送到第二个活动。在第二个活动中获取它通过
将其转换为文件File imgFilePath = new File(imgStringPath);
然后将其转换为位图并将其设置为像这样的
Bitmap myBitmap = BitmapFactory.decodeFile(imgFilePath.getAbsolutePath());
ImageView mImgView = (ImageView)findViewById(R.id.img1);
profilePic3.setImageBitmap(myBitmap);