zoukankan      html  css  js  c++  java
  • 【Android】安卓中常用的图片加载方法

    一、通过相机选图片:


    布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:gravity="center_horizontal">
    
    
    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="使用系统照相机拍照" android:onClick="click"/>
    
    
    <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    
    </LinearLayout>

    代码:

    package uk.ac.essex.camerademo1;
    
    import java.io.File;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.BitmapFactory.Options;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.view.Display;
    import android.view.View;
    import android.widget.ImageView;
    
    public class Camerademo1Activity extends Activity {
        private static final int CAPTURE_PIC = 0;
    
        private ImageView imageView;
    
        private int width;
        private int height;
        private String imageFilePath;
        private Uri imageFileUri;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            imageView = (ImageView) findViewById(R.id.imageView);
            init();
        }
    
        private void init() {
            Display display = getWindowManager().getDefaultDisplay();
            width = display.getWidth();
            height = display.getHeight();
    
            imageFilePath = Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED) ? Environment
                    .getExternalStorageDirectory() + "/1.jpg" : null;
            imageFileUri = Uri.fromFile(new File(imageFilePath));
        }
    
        public void click(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 相机捕捉图片的意图
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);// 指定系统相机拍照保存在imageFileUri所指的位置
            startActivityForResult(intent, CAPTURE_PIC);// 启动系统相机,等待返回
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK && requestCode == CAPTURE_PIC) {
                Options options = new Options();
                options.inJustDecodeBounds = true;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片
                Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸
    
                int widthRatio = (int) Math.ceil(options.outWidth / width);// 获取宽度的压缩比率
                int heightRatio = (int) Math.ceil(options.outHeight / height);// 获取高度的压缩比率
    
                if (widthRatio > 1 || heightRatio > 1) {// 只要其中一个的比率大于1,说明需要压缩
                    if (widthRatio >= heightRatio) {// 取options.inSampleSize为宽高比率中的最大值
                        options.inSampleSize = widthRatio;
                    } else {
                        options.inSampleSize = heightRatio;
                    }
                }
    
                options.inJustDecodeBounds = false;// 设置为真正的解码图片
                bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码图片
    
                imageView.setImageBitmap(bitmap);
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    
    }

    二、通过图库选图片:

    public void Camera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 相机捕捉图片的意图
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);// 指定系统相机拍照保存在imageFileUri所指的位置
    startActivityForResult(intent, 1);// 启动系统相机,等待返回
    }
    
    public void OpenImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, 2);// 打开本地图库
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
    String sdStatus = Environment.getExternalStorageState();
    if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
    Log.i("TestFile", "存储卡读取失败.");
    return;
    }
    // Bundle bundle = data.getExtras();
    // Bitmap bitmap = (Bitmap) bundle.get("data");//
    
    Options options = new Options();
    options.inJustDecodeBounds = true;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片
    Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸
    
    int widthRatio = (int) Math.ceil(options.outWidth / width);// 获取宽度的压缩比率
    int heightRatio = (int) Math.ceil(options.outHeight / height);// 获取高度的压缩比率
    
    if (widthRatio > 1 || heightRatio > 1) {// 只要其中一个的比率大于1,说明需要压缩
    if (widthRatio >= heightRatio) {// 取options.inSampleSize为宽高比率中的最大值
    options.inSampleSize = widthRatio;
    } else {
    options.inSampleSize = heightRatio;
    }
    }
    
    options.inJustDecodeBounds = false;// 设置为真正的解码图片
    bitmap = BitmapFactory.decodeFile(imageFilePath, options);// 解码图片
    
    }
    if (requestCode == 2 && resultCode == RESULT_OK && null != data) {
    Uri uri = data.getData();
    if (!TextUtils.isEmpty(uri.getAuthority())) {
    Cursor cursor = getContentResolver().query(uri,
    new String[] { MediaStore.Images.Media.DATA }, null,
    null, null);
    if (null == cursor) {
    Tools.ToastShort("打开失败,请重试!");
    return;
    }
    cursor.moveToFirst();
    String path = cursor.getString(cursor
    .getColumnIndex(MediaStore.Images.Media.DATA));// 选择的本地图片的路径
    
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    ;// 设置解码只是为了获取图片的width和height值,而不是真正获取图片
    BitmapFactory.decodeFile(path, opts);// 解码后可以options.outWidth和options.outHeight来获取图片的尺寸
    opts.inSampleSize = 10;
    opts.inJustDecodeBounds = false;// 设置为真正的解码图片
    
    try {
    Bitmap bmp = BitmapFactory.decodeFile(path, opts);
    
    } catch (OutOfMemoryError err) {
    
    }

    http://www.eoeandroid.com/thread-3222-1-1.html

    http://www.eoeandroid.com/forum.php?mod=viewthread&tid=6552&page=1

  • 相关阅读:
    shiro注解,初始化资源和权限,会话管理
    shiro标签
    20个为前端开发者准备的文档和指南
    Canvas处理头像上传
    Chrome 实用调试技巧
    JS 代码编一个倒时器
    sql server优化
    在线图片压缩网站
    Request.QueryString
    C#网络爬虫
  • 原文地址:https://www.cnblogs.com/niray/p/3750052.html
Copyright © 2011-2022 走看看