【问题标题】:How to send image from one activity to another in android?如何在android中将图像从一个活动发送到另一个活动?
【发布时间】:2012-07-20 21:08:02
【问题描述】:

我在一个类中有一个 imageView,单击 imageView 时会出现一个对话框,其中有两个选项可以从相机拍摄图像或打开设备的图像库。我想将图像从一个类发送到另一个类,以便它可以出现在 ImageView 中。我从几个小时开始搜索,但我只知道将文本数据从一个班级发送到另一个班级。谁能告诉我如何将图像从一个班级发送到另一个班级?

这是来自发送者类的代码,它将获取图像。

 takeImg.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent 事件) {
                // TODO 自动生成的方法存根
                if(event.getAction() == event.ACTION_UP)
                {
                    i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(i,cameraData);
                }
                返回真;
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO 自动生成的方法存根
        super.onActivityResult(requestCode, resultCode, data);
        如果(结果代码==RESULT_OK)
        {
            捆绑 extras=data.getExtras();
            bmp=(位图)extras.get("数据");
        }
    }

任何帮助谢谢

【问题讨论】:

    标签: android android-imageview


    【解决方案1】:

    您在 Activity 中以 Bitmap 的形式获取 Image,并且您还可以使用 Intent.putExtra() 将其作为 Bitmap 传递给另一个 Activity,如下所示:

    第一个活动

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

    并从 第二个 Activity 中获取,例如:

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

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

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

    【讨论】:

      【解决方案2】:

      我记得 putExtra() 和 getExtra() 的大小限制约为 1mb。所以一张图片可能会超过这个限制。 把路径传给图片怎么样?

      【讨论】:

      • 我没有得到你的观点,请详细解释。
      • 您应该尝试阅读 this entry 并点击其中包含的 Google 群组链接。
      • 基本上归结为“你不能在活动之间传递大于 1 mb 的东西”。
      • 如何发送图片路径?
      • 我认为:stackoverflow.com/questions/7636697/… 是答案。您可以预先传递所需的路径,详情请参阅最后一个答案。
      【解决方案3】:

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

      创建一个类,我们称它为 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();
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          取一个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;

          【讨论】:

          • 你的意思是我应该创建一个全局命名类,然后你能提供什么代码。
          • 是创建1个类声明静态变量并在你的代码中使用如上
          【解决方案6】:

          我会告诉你最好的方法。

          1st) 获取并发送图片 URI

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

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

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

          【讨论】:

            【解决方案7】:

            我不得不稍微重新缩放位图以不超过事务活页夹的 1mb 限制。您可以调整 400 的屏幕或使其动态化,这只是一个示例。它工作正常,质量很好。它也比保存图像并在之后加载它要快得多,但你有大小限制。

            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");
            
            }
            

            我希望我的回答能有所帮助。问候

            【讨论】:

              【解决方案8】:

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

              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;
                  }
              }
              

              在你的第一个 Activity 中放置:

              SingletonModel.getInstance().setImage(image);
              

              在你的第二个活动中:

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

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

              【讨论】:

              • 可以使用SDK提供的Application,为什么还要使用单例?
              猜你喜欢
              • 2016-11-04
              • 2014-11-18
              • 1970-01-01
              • 2016-08-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-21
              相关资源
              最近更新 更多