android开发笔记之多媒体—大图片的加载
博客专区 > Mr_Nice 的博客 > 博客详情
android开发笔记之多媒体—大图片的加载
Mr_Nice 发表于1年前
android开发笔记之多媒体—大图片的加载
  • 发表于 1年前
  • 阅读 1
  • 收藏 0
  • 点赞 2
  • 评论 0

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
博文 47
码字总数 39947
×
Mr_Nice
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: