android studio 打开相册后通过路径显示图片

转载博客:https://blog.csdn.net/diligenttime/article/details/82118452

在image_album_show
设置String ip为全局变量
Intent intent= new Intent();
intent.setClass(image_album_show.this, MainActivity.class);//image_album_show和MainActivity连接起来 允许他们之间传递数据
intent.putExtra(“picture”,ip);//用putExtra把内容传送到另一个Activity,名字是picture,值是ip
startActivity(intent);//启动MainActivity并把intent传递过去
在image_album_show的displayImage中if下添加:ip=imagePath;

在MainActivity
添加一个imageveiw
设置 private ImageView picture1;
在protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    picture1 = (ImageView) findViewById(R.id.imageView);
    Button takePhoto = (Button) findViewById(R.id.take_photo);
    Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
   // Button takeVideo = (Button) findViewById(R.id.take_video);
  // Button showVideo = (Button) findViewById(R.id.show_videos);
    Intent inter=getIntent();

    String imagePath = inter.getStringExtra("picture");

    if (imagePath != null) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        picture1.setImageBitmap(bitmap);
    }
    else {
        Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
    }

android studio 打开相册后通过路径显示图片_第1张图片

你可能感兴趣的:(android studio 打开相册后通过路径显示图片)