【问题标题】:How to show captured Image onto new Activity?如何将捕获的图像显示到新活动上?
【发布时间】:2017-08-08 04:15:58
【问题描述】:

当我单击Button 时,它会打开相机,当我保存它时,它会保存到图库中。我想在新活动中以ImageView 的形式显示该图片。

这是我点击相机按钮的代码

btn2 =(Button)findViewById(R.id.drawer_two);
btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent upload = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivity(upload);
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您的主要活动

     public class MainActivity extends ActionBarActivity {
    
            private final int requestCode = 20;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
    
                Button capturedImageButton = (Button)findViewById(R.id.photo_button);
                capturedImageButton.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(photoCaptureIntent, requestCode);
                    }
                });
            }
    
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if(this.requestCode == requestCode && resultCode == RESULT_OK){
                    Bitmap bitmap = (Bitmap)data.getExtras().get("data");
                    Intent in1 = new Intent(this, Activity2.class);
                    in1.putExtra("image",bitmap);
                     startActivity(in1);
                }
            }
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }
    
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
    
                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }
    
                return super.onOptionsItemSelected(item);
            }
        }
    

    在你的 Activity2 上在 onCreate 方法上写这段代码

    Bundle ex = getIntent().getExtras();
    Bitmap bmp2 = ex.getParceable("image");
    ImageView result = (ImageView)findViewById(R.Id.imageView1);
    result.setImageBitmap(bmp);
    

    【讨论】:

    • 如果我的帖子对你有帮助。那么请接受这个答案
    【解决方案2】:

    使用startActivityForResult()触发Image上传屏幕并使用onActivityResult()取回image数据并将其显示在ImageView

    参考this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多