Android 图片显示实例

来源:互联网 发布:linux 限制ip ssh访问 编辑:程序博客网 时间:2024/06/03 23:47


package com.example.get_path;

import java.io.File;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
 private ImageView img;
 // SD图片路径
 private String filepath = "/sdcard/Photos/IMG0044A.jpg";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  img = (ImageView) findViewById(R.id.imageView1);
  File file = new File(filepath);
  if (file.exists()) {
   Bitmap bm = BitmapFactory.decodeFile(filepath);
   // 将图片显示到ImageView中
   img.setImageBitmap(bm);
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

0 0