Android CV系列 > 画板 可以保存

maven { url "https://jitpack.io" }
compile 'com.github.chenzhenboy:PainterView:0.9'

1.

MainAc XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.as.progressbar.MainActivity">

    <Button
        android:id="@+id/butReset"
        android:text="重新画"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/butSave"
        android:text="保存到本地"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.unco.library.PainterView
        android:id="@+id/painter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

2.MainAc

  Bitmap bitmap = painter.creatBitmap();
                File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
                try {
                    out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                    Toast.makeText(this, "AA"+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                // 其次把文件插入到系统图库
                try {
                    MediaStore.Images.Media.insertImage(getContentResolver(),
                            file .getAbsolutePath(), file.getName(), null);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                // 最后通知图库更新
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(file);intent.setData(uri);
                sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦

权限:

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

猜你喜欢

转载自blog.csdn.net/FlyPig_Vip/article/details/82257134
今日推荐