gpt4 book ai didi

android - 如何向另一个 Activity 显示消息和照片

转载 作者:行者123 更新时间:2023-11-30 01:29:52 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的消息传递应用程序来将文本和照片发送到另一个 Activity ,但我只知道如何显示文本,你能教我如何显示照片吗?我只是一个新的程序员,我想学习所以我希望你能帮助我 :)

这是我的发送 Activity 代码..

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView (R.layout.main);

button = (Button) findViewById(R.id.btn_attach_pic);
imageView = (ImageView) findViewById(R.id.btn_img_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
});

}

public void sendMessage (View view){
message_text = (EditText) findViewById(R.id.editText);
String message = message_text.getText().toString();
Intent intent = new Intent (this, NextActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity (intent);
}

private File getFile(){
File folder = new File("sdcard/UHelp_images");

if(!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"temp.jpg");
return image_file;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/UHelp_images/temp.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}

这是在接收类...

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);

最佳答案

你应该只从其他 Activity 加载它:

File imgFile = new  File("sdcard/UHelp_images/temp.jpg");

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

}

希望对你有帮助。

关于android - 如何向另一个 Activity 显示消息和照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com