首页 文章

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

提问于
浏览
3

我在一个类中有一个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 回答

  • 3

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

    First Activity .

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

    并从 second Activity 得到:

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

    你不需要 get urlload from url .

    这是将 captured image 从一个Activity传递到另一个Activity的最简单方法 .

  • 3

    我记得有一点是putExtra()和getExtra()大小有限,大概是1mb . 因此,图片可能会超出此限制 . 如何将路径传递给图片?

  • 5

    我首选的方式(我认为最直接的方式)是在应用程序中使用自己的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();
    
  • 1

    我得到了将图像路径从一个活动发送到另一个活动所需的答案 . 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);
    
  • 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 ;

  • 2

    我打算告诉你最好的方式 .

    1)获取并发送图像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);
    
  • 2

    我不得不将位图重新缩放一点,以不超过事务 Binders 的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");
    
    }
    

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

  • 3

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

    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对象相同) .

相关问题