1. 程式人生 > >史上最全選擇本地圖片和拍照上傳,超簡單解決獲取不到圖片問題

史上最全選擇本地圖片和拍照上傳,超簡單解決獲取不到圖片問題

相信很多朋友做上傳圖片的時候都苦惱過獲取不到圖片,本篇部落格解決你的煩惱

這裡是選擇本地圖片
try {
                // 選擇本地檔案
                Intent fileIntent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                /* 取得相片後返回本畫面 */
                startActivityForResult(fileIntent, myRequestCode);
} catch (Exception e) { LogUtil.e(e); } //這裡是拍照上傳 // 呼叫系統照相功能 try { // 呼叫系統攝像頭,進行拍照 String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent phoneIntent = new Intent(MediaStore.ACTION
_IMAGE_CAPTURE); String saveDir = GlobalParams.SAVE_PATH; File dir = new File(saveDir); if (!dir.exists()) { dir.mkdir(); } file = new File(saveDir, System.currentTimeMillis
()+".jpg"); phoneIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(phoneIntent, 2); } } catch (Exception e) { LogUtil.e(e); }

下面就開始怎麼獲取圖片了

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == myRequestCode) {
                    Uri uri = data.getData();
                    if (bitmap != null) {
                        bitmap.recycle();
                        bitmap = null;
                        file = null;
                    }
                    if(uri != null){
                        String[] proj = { MediaStore.Images.Media.DATA };
                        Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
                        int actual_image_column_index = actualimagecursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        actualimagecursor.moveToFirst();
                        String img_path = actualimagecursor
                                .getString(actual_image_column_index);
                        file = new File(img_path);
                        if (Integer.parseInt(Build.VERSION.SDK) < 14) {
                            actualimagecursor.close();
                        }
                        ImageSize imageSize = ImageSizeUtil
                                .getImageViewSize(iv_preview);
                        // 2、壓縮圖片
                        // 獲得圖片的寬和高,並不把圖片載入到記憶體中
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeFile(img_path, options);
                        options.inSampleSize = ImageSizeUtil.caculateInSampleSize(
                                options, imageSize.width, imageSize.height);

                        // 使用獲得到的InSampleSize再次解析圖片
                        options.inJustDecodeBounds = false;
                        bitmap = BitmapFactory.decodeFile(img_path, options);
                    }else{
                        //獲取圖片
                        Bundle extras = data.getExtras();
                        bitmap = (Bitmap) extras.get("data");
                    }
                    iv_preview.setImageBitmap(bitmap);

            }else if(requestCode == 2){
                if(file != null){
                    //壓縮圖片
                    compressionImage();
                }
            }
        } catch (Exception e) {
            Log.e("Exception", e.getMessage(), e);
        }
    }

    /**
     * 壓縮圖片
     */
    private void compressionImage() {
        /*ImageSize imageSize = ImageSizeUtil
                .getImageViewSize(iv_preview);*/
        // 2、壓縮圖片
        // 獲得圖片的寬和高,並不把圖片載入到記憶體中
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        /*options.inSampleSize = ImageSizeUtil.caculateInSampleSize(
                options, imageSize.width, imageSize.height);*/
        options.inSampleSize = 8;
        // 使用獲得到的InSampleSize再次解析圖片
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        iv_preview.setImageBitmap(bitmap);
    }

就這麼簡單,就這麼任性搞定選擇圖片和拍照上傳獲取不到圖片問題