gpt4 book ai didi

android - 将图像传递给另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 00:50:05 24 4
gpt4 key购买 nike

多亏了这个网站,我弄清楚了如何在 Activity 之间传递字符串值,但是我在传递图像时遇到了问题。我想要做的是让用户点击一个按钮打开图库并允许选择图片。然后我有另一个按钮打开另一个显示 ImageView 的 Activity 。我希望能够让 ImageView 的图像成为从上一个 Activity 中选择的图像。

这是具有我单击以打开图库并检索所选图像的按钮的类:

public class EnterEdit extends Activity implements View.OnClickListener 
{
private static final int SELECT_IMAGE = 0;

String filepath;

Bundle fieldresults;
Intent b;

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

Button selectwallpaper = (Button) findViewById(R.id.selectwallpaper);
selectwallpaper.setOnClickListener(this);

Button previewwallpaper = (Button) findViewById(R.id.previewwallpaper);
previewwallpaper.setOnClickListener(this);

fieldresults = new Bundle();
b = new Intent(this, PreviewScreen.class);
}


@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.selectwallpaper:
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, SELECT_IMAGE);
break;

case R.id.previewwallpaper:
startActivity(b);
}
break;
}


protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_IMAGE)
{
Uri selectedimage = data.getData();
String[] filepathcolumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedimage, filepathcolumn, null, null, null);
cursor.moveToFirst();

int columnindex = cursor.getColumnIndex(filepathcolumn[0]);
filepath = cursor.getString(columnindex);
cursor.close();

fieldresults.putString("bitmap", filepath);
b.putExtras(fieldresults);
}
}
}
}

这里是应该显示所选图像的类:

public class PreviewScreen extends Activity implements View.OnClickListener 
{

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

Bundle fieldresults = this.getIntent().getExtras();
String backgroundpath = fieldresults.getString("bitmap");

String background = BitmapFactory.decodeFile(backgroundpath);
ImageView gallerypic = (ImageView) findViewById(R.id.gallerypic);
gallerypic.setImageBitmap(background);
}
}

如果我应该传递 selectedImageb.putExtra("bitmap", selectedimage); 行。我都试过了,但我没有看到第二个 Activity 的图像。此外,我不确定在 PreviewScreen 类中是否正确设置了 imageview。任何帮助表示赞赏。谢谢。

最佳答案

相信我,您不想在 Activity 之间传递图像。相反,为什么不简单地传递图像的路径并让第二个 Activity 决定如何处理该路径(在本例中解码和显示图像)。

关于android - 将图像传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3544660/

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