当我删除文件并尝试再次保存它时为空 uri

Null uri when i delete the file and i try to save it again

我正在尝试将 screen 的所有内容保存为 image 格式,为此在我的 layout I有一个 LinearLayout,其中添加了所有其他元素。

<LinearLayout
        android:id="@+id/creado"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

然后,创建:

private LinearLayout contenido;

并在onCreate()方法中调用:

contenido = (LinearLayout)findViewById(R.id.creado);

要存储Layout的所有内容,我使用 setOnLongClickListener事件:

    contenido.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if(permissionHelper.hasPermission()){
                saveImage(VistaPrevia.this);
            }else{
                ejecutar();
            }
            return true;
        }
    });

为了保存它,我采用了以下方法:

private void GuardarLayout(Context context){//method to save
    contenido.setDrawingCacheEnabled(true);
    contenido.buildDrawingCache();
    Bitmap bmap = contenido.getDrawingCache();
    try {
        saveImage(bmap);
    } catch (Exception e) {
        Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    } finally {
        contenido.destroyDrawingCache();
    }
}

private void saveImage(Bitmap bitmap) {
    if (android.os.Build.VERSION.SDK_INT >= 29) {
        ContentValues values = contentValues();
        values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + "Genshin Impact Mis Builds");
        values.put(MediaStore.Images.Media.IS_PENDING, true);
        Uri uri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        if (uri != null) {
            Log.d("HOLAIF", "saveImage: " + uri.toString());
            try {
                saveImageToStream(bitmap, this.getContentResolver().openOutputStream(uri));
                values.put(MediaStore.Images.Media.IS_PENDING, false);
                this.getContentResolver().update(uri, values, null, null);
                Toast.makeText(this, "¡Se ha guardado tu build de manera exitosa!", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    } else {
        File directory = new File(Environment.getExternalStorageDirectory().toString() + '/' + getString(R.string.app_name));
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String fileName = nombrePersonaje + ".jpg";
        File file = new File(directory, fileName);
        try {
            saveImageToStream(bitmap, new FileOutputStream(file));
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
            this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

private ContentValues contentValues() {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DISPLAY_NAME, nombrePersonaje + ".jpg");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");
    values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    }
    return values;
}

private void saveImageToStream(Bitmap bitmap, OutputStream outputStream) {
    if (outputStream != null) {
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

问题是救了一个之后就不让我救别人了。我的意思是,我可以保存一个。当我想保留另一个时,它不会让我。当我关闭并重新打开应用程序时会反映出此问题。我只能保存一次,然后就不再保存了。当我第一次保存 layouturi 不是 null:

但是当我关闭应用程序并重新启动时,我无法保存并且 urinull

为什么 Uri 变成了 null?我在代码中写错了什么?从已经非常感谢你

更新

我发现我的代码的问题是我删除了保存的文件。那是文件保存的时间,所以我将在您的 repesctive 位置搜索文件并将其删除,然后我将转到我的应用程序并尝试再次保存它,这是 uri 为空的时候。我该如何解决这个问题?

这个错误主要取决于ContentProvider

找到下面的工作代码=>

private File saveBitmap(Bitmap bitmap){
    try {
        File path =new File(
                this.getFilesDir(),
                getString(R.string.app_name)
        );
        if (!path.exists()) {
            path.mkdirs();
        }

        String fileName = "filename.png";

        File outFile =new File(path, fileName);
        if (!outFile.exists()) {
            FileOutputStream outputStream = new FileOutputStream(outFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 70, outputStream);
            outputStream.close();
        }
        return outFile;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

确保验证 provider_path.xml

<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <files-path
        name="0"
        path="." />

</paths>