解決Android7.0打開相機和裁剪問題

今天手機升級Android7.0之後,發現,切換頭像出錯了。一臉懵逼,猜測確定是升級Android7.0之後,又有了新的改動了,(雖然知道Android7.0會有改動,可是沒想到,會在相機這麼重要的功能上不兼容7.0如下)
一查發現,原來是由於7.0的打開相機的方式改變了,再也不是Uri.fromFile(),那麼7.0採用的是什麼方式呢?
FileProvider沒錯,就是FileProvider:java

使用FileProvider的方式打開相機

主要原理就是在應用間共享文件:
首先在AndroidManifest.xml的application中配置下面代碼android

<!--android7.0打開相機配置方法-->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="opencamera.baochen.com.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

其次在res文件下面建立一個xml文件夾,在其中建立file_paths.xml文件。
file_paths.xmlcanvas

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--    path=""表明根目錄   path="images"(eg:/storage/emulated/0/images)
            name: 表明定義在Content中的字段
            若果 path="images";   name="camera_photos"
            那麼:
            content://opencamera.baochen.com.fileprovider/images/camera_photos.jpg
            同時也是:/storage/emulated/0/images/camera_photos.jpg
這裏我設置在根目錄,即path=""
    -->
    <paths>
        <external-path
            name="image"
            path="" />
    </paths>
</resources>

下面就是Android打開相機的辦法了。服務器

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open_camera://打開相機
                Intent openCameraIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //Android7.0以上
                    uri = FileProvider.getUriForFile(MainActivity.this, "opencamera.baochen.com.fileprovider", new File(Environment
                            .getExternalStorageDirectory(), "image.jpg"));//經過FileProvider建立一個content類型的Uri

                    openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目標應用臨時受權該Uri所表明的文件
                    openCameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設置Action爲拍照
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//將拍取的照片保存到指定URI
                    startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
                } else {
                    uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));//7.0如下打開相機拍照的方法
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//保存照片
                    startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
                }

                break;
            case R.id.open_album://打開相冊
                Intent openAlbumIntent = new Intent(
                        Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                startActivityForResult(openAlbumIntent, TAG_PHOTO_ALBUM);
                break;
        }
    }

裁剪:app

/**
     * 裁剪圖片方法實現
     *
     * @param uri
     */
    protected void startPhotoZoom(Uri uri) {
        if (uri == null) {
            Log.i("tag", "The uri is not exist.");
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        //這裏的uri是從親們更具不一樣的方式得到的,因此才裁剪的時候,無需再進行Android7.0判斷.
        intent.setDataAndType(uri, "image/*");
        // 設置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是寬高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪圖片寬高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

裁剪後顯示的圖片

文件夾根目錄中保存的圖片ide

所有代碼:
MainActivity.javaui

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button openCamera;
    Button openAlbum;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);
        openCamera = (Button) findViewById(R.id.open_camera);
        openAlbum = (Button) findViewById(R.id.open_album);
        openCamera.setOnClickListener(this);
        openAlbum.setOnClickListener(this);
    }

    private static final int TAG_PHOTO_CAMERA = 0;//表明相機
    private static final int TAG_PHOTO_ALBUM = 1;//表明相冊
    private static final int CROP_SMALL_PICTURE = 2;//裁剪照片
    protected static Uri uri;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open_camera://打開相機
                Intent openCameraIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //Android7.0以上
                    uri = FileProvider.getUriForFile(MainActivity.this, "opencamera.baochen.com.fileprovider", new File(Environment
                            .getExternalStorageDirectory(), "image.jpg"));//經過FileProvider建立一個content類型的Uri

                    openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目標應用臨時受權該Uri所表明的文件
                    openCameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設置Action爲拍照
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//將拍取的照片保存到指定URI
                    startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
                } else {
                    uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));//7.0如下打開相機拍照的方法
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//保存照片
                    startActivityForResult(openCameraIntent, TAG_PHOTO_CAMERA);
                }

                break;
            case R.id.open_album://打開相冊
                Intent openAlbumIntent = new Intent(
                        Intent.ACTION_GET_CONTENT);
                openAlbumIntent.setType("image/*");
                startActivityForResult(openAlbumIntent, TAG_PHOTO_ALBUM);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) { // 若是返回碼是能夠用的
            switch (requestCode) {
                case TAG_PHOTO_CAMERA:
                    startPhotoZoom(uri); // 開始對圖片進行裁剪處理(相機)
                    break;
                case TAG_PHOTO_ALBUM:
                    if (data != null) {
                        startPhotoZoom(data.getData()); // 開始對圖片進行裁剪處理(相冊)
                    }
                    break;
                case CROP_SMALL_PICTURE:
                    if (data != null) {
                        setImageToView(data); // 讓剛纔選擇裁剪獲得的圖片顯示在界面上
                    }
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 裁剪圖片方法實現
     *
     * @param uri
     */
    protected void startPhotoZoom(Uri uri) {
        if (uri == null) {
            Log.i("tag", "The uri is not exist.");
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        //這裏的uri是從親們更具不一樣的方式得到的,因此才裁剪的時候,無需再進行Android7.0判斷.
        intent.setDataAndType(uri, "image/*");
        // 設置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是寬高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪圖片寬高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

    /**
     * 保存裁剪以後的圖片數據
     *
     * @param
     * @param
     */
    protected void setImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            photo = AvatarUtils.toRoundBitmap(photo, uri); // 這個時候的圖片已經被處理成圓形的了
            image.setImageBitmap(photo);
            //上傳到服務器等操做...
        }
    }
}

activity_main.xmlthis

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
         android:id="@+id/image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/open_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="相機"/>
    <Button
        android:id="@+id/open_album"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="相冊"/>

</LinearLayout>

AvatarUtils.javaspa

public class AvatarUtils {
    /**
     * Save image to the SD card
     *
     * @param photoBitmap
     * @param photoName
     * @param path
     */
    public static String savePhoto(Bitmap photoBitmap, String path,
                                   String photoName) {
        String localPath = null;
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            File photoFile = new File(path, photoName + ".png");
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(photoFile);
                if (photoBitmap != null) {
                    if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,
                            fileOutputStream)) { // 轉換完成
                        localPath = photoFile.getPath();
                        fileOutputStream.flush();
                    }
                }
            } catch (FileNotFoundException e) {
                photoFile.delete();
                localPath = null;
                e.printStackTrace();
            } catch (IOException e) {
                photoFile.delete();
                localPath = null;
                e.printStackTrace();
            } finally {
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                        fileOutputStream = null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return localPath;
    }

    /**
     * 轉換圖片成圓形
     *
     * @param bitmap
     *            傳入Bitmap對象
     * @param tempUri
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap, Uri tempUri) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = width / 2;
            left = 0;
            top = 0;
            right = width;
            bottom = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right,
                (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top,
                (int) dst_right, (int) dst_bottom);
//        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);// 設置畫筆無鋸齒

        canvas.drawARGB(0, 0, 0, 0); // 填充整個Canvas
        paint.setColor(color);

        // 如下有兩種方法畫圓,drawRounRect和drawCircle
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//
        // 畫圓角矩形,第一個參數爲圖形顯示區域,第二個參數和第三個參數分別是水平圓角半徑和垂直圓角半徑。
        canvas.drawCircle(roundPx, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));// 設置兩張圖片相交時的模式,參考http://trylovecatch.iteye.com/blog/1189452
        canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合併bitmap和已經draw了的Circle

        return output;
    }

}

end-----------------------------------------------------------code

相關文章
相關標籤/搜索