欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Bitmap保存图片到指定路径

程序员文章站 2022-06-15 11:55:35
1.manifests文件需要增加权限。2.在Android Q设备上有两种方式来让分区存储生效:以 Android 9 或更低版本为目标平台 (Target SDK <=28)如果Target SDK > 28,请在manifest中添加android:requestLegacyExternalStorage=“true”。...

1.manifests文件需要增加权限。

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

2.在Android Q设备上有两种方式来让分区存储生效:以 Android 9 或更低版本为目标平台 (Target SDK <=28)如果Target SDK > 28,请在manifest中添加android:requestLegacyExternalStorage=“true”。

    <application
        android:allowBackup="true"
      ...
        android:requestLegacyExternalStorage="true"
        android:theme="@style/AppTheme">

3.获取外部存储路径,定义图片的存储路径,判断图片的存储路径是否存在,不存在则建立。

private static final String SD_PATH = Environment.getExternalStorageDirectory().getPath() + "/fileimage/";
private static final String IN_PATH = Environment.getExternalStorageDirectory().getPath() + "/fileimage/";   
 public static String saveToBitmapfile(Bitmap bmp, String desFileName, Context context) {
        String savePath;
        File file;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            savePath = SD_PATH;
        } else {
            savePath = IN_PATH;
        }
        try {
            file = new File(savePath + generateFileName() + ".JPEG");
            if (!file.exists()) {
                file.getParentFile().mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            Toast.makeText(context, "保存成功,位置:" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return file.getAbsolutePath();
    }
    private static String generateFileName() {
        return UUID.randomUUID().toString();
    }

4.activity实现code,通过截屏方式,并指定路径进行保存。

public class TestActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //请求写入权限
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                1001);
        Button screenBt = (Button) findViewById(R.id.screen_button);
        screenBt.setOnClickListener(new View.OnClickListener() {
            private Bitmap mBitmap;
            @Override
            public void onClick(View v) {
            //截屏生成bitmap
                mBitmap=ScreenWindow();
                //指定路径,并进行保存
                saveToBitmapfile(mBitmap,System.currentTimeMillis() + "",getApplicationContext());
            }
        });
    }
    public Bitmap ScreenWindow() {
        getWindow().getDecorView().setDrawingCacheEnabled(true);
        Bitmap bmp = getWindow().getDecorView().getDrawingCache();
        return bmp;
    }
    //重写权限回调
        @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length == 0 || PackageManager.PERMISSION_GRANTED != grantResults[0]) {
            Toast.makeText(this, "你拒绝了权限,无法创建!", Toast.LENGTH_LONG).show();
        }
    }

本文地址:https://blog.csdn.net/xiao_yuanjl/article/details/107897901

相关标签: android 研发