gpt4 book ai didi

android - 将位图图像从一个 Activity 传递到另一个 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:34 30 4
gpt4 key购买 nike

在我的应用程序中,我显示了图库中的图片数量,只要我从中选择一张图片,该图片就应该发送到新 Activity ,在该 Activity 中,所选图片将被设置为背景。但是,我能够从图库中获取图像,但一旦我选择一个应用程序就会崩溃。提前致谢

Activity-1(图片显示在图库中,选中的图片发送到新的activity)

public class Gallery extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);

Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);


buttonLoadImage.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}



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

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {






Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);


// Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage);
Intent intent = new Intent(Gallery.this,GesturesActivity.class);
intent.putExtra("bmp",croppedImage);
startActivity(intent);

Log.v("sending image","sending image");


}


}
}

Activity -1(XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
<Button
android:layout_height="wrap_content"
android:text="Load Picture"
android:layout_width="wrap_content"
android:id="@+id/buttonLoadPicture"
android:layout_weight="0"
android:layout_gravity="center"></Button>
</LinearLayout>

Activity-2(选中的图片设置为屏幕背景图片的activity)

  public class GesturesActivity extends Activity {


private final int MENU_CAMERA = Menu.FIRST;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);


Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp");
BitmapDrawable background = new BitmapDrawable(bmp);
getWindow().setBackgroundDrawable(background); //background image of the screen



Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert);
View view = new SandboxView(this, bitmap);

setContentView(view);
}



public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();

menu.add(0, 11, 0, "Take Snapshot");

return super.onPrepareOptionsMenu(menu);
}


public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}



}

最佳答案

有 3 种解决方案可以解决此问题。

1) 首先将图像转换为字节数组,然后传递给 Intent,在下一个 Activity 中,从 Bundle 中获取字节数组并转换为图像(位图)并设置到 ImageView 中。

将位图转换为字节数组:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

将字节数组传递给 Intent :-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从 Bundle 中获取字节数组并转换为位图图像:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 首先将图像保存到 SDCard 中,然后在下一个 Activity 中将此图像设置到 ImageView 中。

3) 将 Bitmap 传递到 Intent 并在下一个 Activity 中从 bundle 中获取位图,但问题是如果您的 Bitmap/Image 尺寸当时很大,则图像不会在下一个 Activity 中加载。

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

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