android 保存图片到本地,在相册显示出来

首先在清单注册权限

	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后在你的动态申请权限组上加上这两个权限。
首先把View转成Bitmap

		//计算设备分辨率
        WindowManager  manager    = activity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;

        // 整个View的大小 参数是左上角 和右下角的坐标
        view.layout(0, 0, width, height);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);

        //测量,布局View
        view.measure(measuredWidth, measuredHeight);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        view.setDrawingCacheBackgroundColor(activity.getResources().getColor(R.color.color_F9F9F9));

        // 把一个View转换成图片
        Bitmap cacheBmp = viewConversionBitmap(view,activity);
        view.destroyDrawingCache();

然后在得到bitmap保存到应用的本地内存,这里如果找手机里文件夹可能会报错,因为谷歌不让在非应用的文件夹下创建文件。
下面是把bitmap保存到本地。

String sdCardDir = activity.getExternalFilesDir(null).getAbsolutePath() + File.separator + "JstImage" + File.separator;
        File appDir = new File(sdCardDir);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            cacheBmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(activity.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri    uri    = Uri.fromFile(file);
        intent.setData(uri);
        activity.sendBroadcast(intent);

下面是我封装的一个方法:

private static final String TAG = "ViewUtils";

    /**
     * 保存view为图片
     *
     * @param activity     Activity
     * @param view         View
     */
    public static boolean saveView(Activity activity, View view) {

        //计算设备分辨率
        WindowManager  manager    = activity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;

        // 整个View的大小 参数是左上角 和右下角的坐标
        view.layout(0, 0, width, height);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);

        //测量,布局View
        view.measure(measuredWidth, measuredHeight);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        view.setDrawingCacheBackgroundColor(activity.getResources().getColor(R.color.color_F9F9F9));

        // 把一个View转换成图片
        Bitmap cacheBmp = viewConversionBitmap(view,activity);
        view.destroyDrawingCache();
        String sdCardDir = activity.getExternalFilesDir(null).getAbsolutePath() + File.separator + "JstImage" + File.separator;
        File appDir = new File(sdCardDir);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            cacheBmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(activity.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri    uri    = Uri.fromFile(file);
        intent.setData(uri);
        activity.sendBroadcast(intent);
        return true;
    }


    /**
     * view转bitmap
     *
     * @param v View
     * @return Bitmap
     */
    private static Bitmap viewConversionBitmap(View v,Activity activity) {
        int w = v.getWidth();
        int h = v.getHeight();
        Log.e(TAG, "width: " + w + " height: " + h);
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c   = new Canvas(bmp);

        c.drawColor(activity.getResources().getColor(R.color.color_F9F9F9));
        /** 如果不设置canvas画布为白色,则生成透明 */

        v.layout(0, 0, w, h);
        v.draw(c);

        return bmp;
    }

你可能感兴趣的:(android 保存图片到本地,在相册显示出来)