如何在Android中将图像从一个活动发送到另一个活动?

时间:2012-07-22 08:40:10

标签: android android-imageview

我在一个类中有一个imageView,并且在单击imageView时会出现一个对话框,其中有两个选项可以从相机中获取图像或打开设备的图库。我想将图像从一个类发送到另一个类,以便它可以出现在ImageView中。我从很多小时开始搜索,但我只是将文本数据从一个类发送到另一个类。任何人都可以告诉我们将图像从一个类发送到另一个类吗?

这是来自发件人类的代码,它将拍摄图像。

   takeImg.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                return true;
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras=data.getExtras();
            bmp=(Bitmap)extras.get("data");
        }
    }

感谢任何帮助

8 个答案:

答案 0 :(得分:5)

您在活动中将图片作为位图获取,并且您还将其作为位图传递给另一个位于 Intent.putExtra()的位图,如下所示:

第一项活动

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bmp_Image", bmp); 

第二个活动获取,如:

Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image"); 

您不需要获取网址从网址加载

这是将捕获的图像从一个Activity传递到另一个Activity的最简单方法。

答案 1 :(得分:3)

我记得putExtra()和getExtra()的大小限制大约为1mb。所以图片可能会超出此限制。 如何将路径传递给图片?

答案 2 :(得分:3)

我首选的方式(我认为最直接的方式)是在应用程序中使用自己的Application实例,以存储多于1个活动共有的变量。

创建一个类,让我们称之为MainApplication,扩展android.app.Application 并在清单中声明:

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name=".MainApplication">

然后在Activity中获取应用程序对象的实例,如下所示:

MainApplication application = ((MainApplication)getApplication());

在此应用程序对象中,您可以存储任何应用程序级数据并照常使用它:

application.setImage(...);

application.getImage();

答案 3 :(得分:3)

我得到了将图像路径从一个活动发送到另一个活动所需的答案。 filePath是图像的路径。

Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);

在另一项活动中获取路径

final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);

答案 4 :(得分:2)

选择一个Global.class并声明public static Bitmap bmp;

takeImg.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                return true;
            }
        });
    }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            Bundle extras=data.getExtras();
            Global.bmp=(Bitmap)extras.get("data");
        }
    }

当你想使用Bitmap bitmap = Global.bmp;

答案 5 :(得分:2)

我会告诉你最好的方式。

1st)获取并发送图像URI

Uri imageUri = data.getData();
Intent newIntent = new Intent(Class.this, Class.class);
newIntent.putExtra(IMAGE_URI_KEY, imageUri);
startActivity(newIntent);

2)接收图像以及如何显示

receivedImageUri = getIntent().getParcelableExtra(IMAGE_URI_KEY);
imageView.setImageURI(receivedImageUri);

答案 6 :(得分:2)

我不得不将位图重新缩放一点,以不超过事务绑定器的1mb限制。你可以调整400你的屏幕或使它成为dinamic它只是一个例子。它工作正常,质量很好。它也比保存图像和加载它后快得多,但你有尺寸限制。

public void loadNextActivity(){
Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = returnScaledBMP();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

confirmBMP.putExtra("Bitmap",bmp);
startActivity(confirmBMP);
finish();

}
public Bitmap returnScaledBMP(){
Bitmap bmp=null;
bmp = tempBitmap;
bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
return bmp;
}

使用以下代码恢复nextActivity中的bmp后:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirmBMP);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");

}

我希望我的答案在某种程度上有所帮助。问候

答案 7 :(得分:1)

您可以使用Singleton对象来存储您的图像:

public class SingletonModel {
    private Bitmap Image;
    private SingletonModel; 
    public static SingletonModel getInstance() {
        if (instance == null) {
            instance = new SingletonModel();
        }
        return instance;
    }

    public Bitmap getImage() {
       return this.Image
    }

    public Bitmap setImage(Bitmap ImageIn) {
        this.Image = ImageIn;
    }
}

在你的第一个活动中:

SingletonModel.getInstance().setImage(image);

在你的第二个活动中:

Bitmap image = SingletonModel.getInstance().getImage();

另外,您可以创建一个扩展Application的Object,因此该Object对于所有类都是可见的(这个想法与Singleton对象相同)。