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");
}
}
}
<?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(将所选图像设置为屏幕背景图像的活动)
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,在下一个活动中,从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 = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
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);
在SO上也有类似的问题,但没有一个对我有效。 我想在Activity1中获取被点击的图像并在Activity2中显示它。 我获取被点击图像的图像id如下所示: 并通过意图传递给另一个活动。 任何帮助都很感激。
我想把意图转移到Xamarin.Android中的另一个活动。基本上,我需要Intent.data和Intent.clipdata到另一个活动,所以我使用下面的代码来传输Intent,但我不知道检索它的正确方法。 下面是Main Activity中的代码 在第二活动中 如何在第二个活动中检索意图?
我试图将图像从一个活动发送到另一个活动,但我不知道如何设置ImageView。
在活动1中,我将一张图片从url加载到imageview中(使用glide)。当我切换到活动2时,我断开了网络连接,我需要在另一个imageview中加载相同的图像。我该如何实现这一点?使用glide缓存在某处的图像可以做到这一点吗?
我尝试使用 如有任何帮助,不胜感激,谢谢。