android开发笔记之多媒体—小图片的加载

1.将图片放入SD卡中

android开发笔记之多媒体—小图片的加载_第1张图片

2.从SD卡取出该图片

String path = Environment.getExternalStorageDirectory() + "/img_small_1.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(path);

3.将图片显示到ImageView组件上

iv.setImageBitmap(bitmap);

4.最后记得加上权限

//读SD卡的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <!-- -->
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>

核心代码:

/**
* 从SD卡中加载小图片
* @author Administrator
*
*/
public class MainActivity extends Activity {

private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.iv);
}
//从SD卡中加载图片
public void loadimage(View view) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//判断SD卡是否可用
        String path = Environment.getExternalStorageDirectory() + "/img_small_1.jpg";
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        iv.setImageBitmap(bitmap);
    }else {
        Toast.makeText(this, "SD卡不存在或不可用", Toast.LENGTH_LONG).show();
    }
}

}

“`

结果:

android开发笔记之多媒体—小图片的加载_第2张图片

你可能感兴趣的:(Android开发,多媒体)