[英]Passing an image from an activity to another one
我想在單擊按鈕時將圖像從一個活動傳遞到另一個活動。 我在第一個活動中有一個按鈕“顯示圖像”。 當我單擊它時,它應該從項目的mipmap文件夾中傳遞兩個圖像,然后轉到第二個活動,並在該活動的ImageView上顯示傳遞的圖像之一。 在第二個活動中,我有兩個按鈕,應該分別接收圖像並在每個按鈕上單擊時顯示這些圖像。 我嘗試使用Intent傳遞圖像,但是它沒有用。 還有其他方法可以將圖像從mipmap文件夾從一個活動發送到另一個活動嗎?
這是我的代碼: MainActivity.java
package com.example.abina.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showmyImage();
}
});
}
public void showmyImage(){
Intent intent = new Intent(this, Main2Activity.class);
Bitmap bitmap; // your bitmap
bitmap = null;
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intent.putExtra("byteArray", _bs.toByteArray());
startActivity(intent);
}
}
activity_main.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="137dp"
android:text="Show Image" />
</RelativeLayout>
Main2Activity.java
package com.example.abina.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class Main2Activity extends AppCompatActivity {
Button image1;
Button image2;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
image1 =(Button) findViewById(R.id.image1);
image2 = (Button) findViewById(R.id.image2);
imageView =(ImageView) findViewById(R.id.imageView);
if(getIntent().hasExtra("byteArray")) {
Bitmap _bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(_bitmap);
}
}
}
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<Button
android:id="@+id/image1"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Image1" />
<Button
android:id="@+id/image2"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="0dp"
android:text="Image2" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
首先,mipmap文件夾僅用於放置您的應用程序/啟動器圖標(顯示在主屏幕上)。 您使用的任何其他可繪制資產應放置在相關的可繪制文件夾中。
接下來,因為它們將出現在您的可繪制對象中,所以我只傳遞@DrawableRes id
例如R.id.image_name
值。
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);
另外,我建議您使用public static
變量IMAGE_RES_ID_KEY
作為您的額外密鑰,以避免輸入錯誤。
然后在另一邊,您可以簡單地
if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.