美文网首页
Android 截屏保存到相册没有更新的解决方案

Android 截屏保存到相册没有更新的解决方案

作者: 简单就是真 | 来源:发表于2023-08-21 16:23 被阅读0次

1.今天做了个小需求,实现截屏总是出现崩溃问题
是因为在下面的方法中

private void snapCurrentWindow() {

    int width = DimensionUtil.getWidth(this);
    int height = DimensionUtil.getHeight(this);
    Log.e("截屏功能", "snapCurrentWindow: " + width + height);
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    View screenView = getWindow().getDecorView();

    screenView.setDrawingCacheEnabled(true);

    bitmap = screenView.getDrawingCache();

    //先判断权限
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            return;
        }else {
            saveImageToGallery(this, bitmap);
        }
    }else {
        saveImageToGallery(this, bitmap);
    }

    Log.d(TAG, "snapCurrentWindow: 000" + bitmap  + "width" + width + "height" + height );

// saveImageToGallery(this, bitmap);
}
1.闪退报错: getWindow().getDecorView() 为null
2.保存到相册,日期没有同步
报错原因:是因为实在onCreate()方法里面,还没初始化化完成,导致获取不到
解决方法:不在onCreate()方法里执行
下面是方法:
/**

  • 保存截屏到相册

  • @param context

  • @param bmp
    */
    public void saveImageToGallery(Context context, Bitmap bmp) {

    // 首先保存图片
    String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Wancms" + File.separator;
    File appDir = new File(storePath);
    if (!appDir.exists()) {
    appDir.mkdir();
    }
    String fileName = "Screenshot_" + System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
    try {
    FileOutputStream fos = new FileOutputStream(file);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    // 其次把文件插入到系统图库
    try {
    String uriString = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
    File file1 = new File(getRealPathFromURI(Uri.parse(uriString), context));
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file1)));
    Toast.makeText(LoginActivity.this, "已保存到相册", Toast.LENGTH_SHORT).show();

    } catch (FileNotFoundException e) {
    Toast.makeText(LoginActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
    e.printStackTrace();
    }

}

//得到绝对地址
private static String getRealPathFromURI(Uri contentUri,Context context) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String fileStr = cursor.getString(column_index);
cursor.close();
return fileStr;
}
getRealPathFromURI() 这个方法就能完美的解决相册日期没有 同步的问题了

相关文章

网友评论

      本文标题:Android 截屏保存到相册没有更新的解决方案

      本文链接:https://www.haomeiwen.com/subject/mfcymdtx.html