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

1.将图片导入到SD卡

这里写图片描述

2.从SD中获取图片

String path = Environment.getExternalStorageDirectory() + "/img_big_1.jpg";
bitmap = BitmapFactory.decodeFile(path, options);

3.为这种图片设置采样率(即压缩比)

Options options = new Options();
            //设置图片的采样率
            options.inSampleSize = scale;  //假设为4,表示原来宽高的1/4,原来为2560*1600变成了640*400

4.最后将图片显示到ImageView组件上

iv.setImageBitmap(bitmap);

5.加上相应权限

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

不过我们这次这个采样率得计算一下,因为我们需要让图片以最合适的大小显示到当前屏幕上:

private Bitmap getScaleBitmap(String path) {
        Bitmap bitmap = null;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            imageWidth = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
            imageHeight = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
            int scaleX = (int) (imageWidth*0.1f/screenWidth + 0.5f); //加0.5f是为了四舍五入
            int scaleY = (int) (imageHeight*0.1f/screenHeight + 0.5f);
            int scale = (int)( Math.sqrt(scaleX*scaleX + scaleY*scaleY) + 0.5f);
            Options options = new Options();
            //设置图片的采样率
            options.inSampleSize = scale;  //假设为4,表示原来宽高的1/4,原来为2560*1600变成了640*400
            bitmap = BitmapFactory.decodeFile(path, options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

当然要知道这个比,就还应该得到屏幕的宽高

// 获取Android屏幕的信息
    private void getScreenWith() {
        //三种方式
        //第一种
//      WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//      width = windowManager.getDefaultDisplay().getWidth();
//      height = windowManager.getDefaultDisplay().getHeight();

        //第二种
//      Point point = new Point();
//      WindowManager windowManager = (WindowManager)       getSystemService(WINDOW_SERVICE);
//      windowManager.getDefaultDisplay().getSize(point);
//      width = point.x;
//      height = point.y;

        //第三种
        DisplayMetrics metric = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metric);
        screenWidth = metric.widthPixels;   //屏幕宽度
        screenHeight = metric.heightPixels;  //屏幕高度
    }

核心代码:

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

    private ImageView iv;
    private int screenWidth,screenHeight; //屏幕的宽高
    private int imageWidth,imageHeight;   //图片的宽高
    @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)) {
            String path = Environment.getExternalStorageDirectory() + "/img_big_1.jpg";
            getScreenWith();
            Bitmap bitmap = getScaleBitmap(path);
            iv.setImageBitmap(bitmap);
        }else {
            Toast.makeText(this, "SD卡不存在或不可用", Toast.LENGTH_LONG).show();
        }
    }
    private Bitmap getScaleBitmap(String path) {
        Bitmap bitmap = null;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            imageWidth = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
            imageHeight = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
            int scaleX = (int) (imageWidth*0.1f/screenWidth + 0.5f); //加0.5f是为了四舍五入
            int scaleY = (int) (imageHeight*0.1f/screenHeight + 0.5f);
            int scale = (int)( Math.sqrt(scaleX*scaleX + scaleY*scaleY) + 0.5f);
            Options options = new Options();
            //设置图片的采样率
            options.inSampleSize = scale;  //假设为4,表示原来宽高的1/4,原来为2560*1600变成了640*400
            bitmap = BitmapFactory.decodeFile(path, options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
// 获取Android屏幕的信息
    private void getScreenWith() {
        //三种方式
        //第一种
//      WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//      width = windowManager.getDefaultDisplay().getWidth();
//      height = windowManager.getDefaultDisplay().getHeight();
        //第二种
//      Point point = new Point();
//      WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//      windowManager.getDefaultDisplay().getSize(point);
//      width = point.x;
//      height = point.y;
        //第三种
        DisplayMetrics metric = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        screenWidth = metric.widthPixels;   //屏幕宽度
        screenHeight = metric.heightPixels;  //屏幕高度
    }
}

结果:

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值