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

1.将图片导入到SD卡

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

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;  //屏幕高度
    }
}

结果:

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