Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發步步為營之66:android圖片選取

android開發步步為營之66:android圖片選取

編輯:關於Android編程

最近做一個頁面,反饋問題頁面,有個用戶上傳問題圖片的功能。本來很笨的想把系統的所有圖片列出來,然後讓用戶選擇,後來發現原來可以直接打開手機所有圖片的api的。效果如圖:

\

給出主要代碼:

1、選擇圖片

 

    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, 1);


 

2、獲取圖片

 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(picturePath, opts);
                opts.inSampleSize = computeSampleSize(opts, -1, 160 * 160);//轉換成160*160大小圖片

                // 這裡一定要將其設置回false,因為之前我們將其設置成了true
                opts.inJustDecodeBounds = false;
                try {
                    Bitmap bmp = BitmapFactory.decodeFile(picturePath, opts);
                    if (mPosition != 0)// 替換
                    {
                        fba.getmImgs().set(mPosition, bmp);
                        fba.getmImgPaths().set(mPosition, picturePath);
                    } else {
                        fba.getmImgs().add(bmp);
                        fba.getmImgPaths().add(picturePath);
                    }

                } catch (OutOfMemoryError err) {
                    if (err != null) {
                        err.printStackTrace();
                    }
                }
                fba.notifyDataSetChanged();

            }

        } catch (Exception e) {
            if (e != null) {
                e.printStackTrace();
            }
        } catch (OutOfMemoryError e) {
            if (e != null) {
                e.printStackTrace();
            }
        }

    }
   public int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved