【发布时间】:2021-07-04 08:49:11
【问题描述】:
我正在尝试以 image 格式保存 screen 的所有内容,为此在我的 layout 我有一个 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();
}
}
}
问题是,救了一个之后,就不让我救别人了。我的意思是,我可以保存一个。当我想保留另一个时,它不会让我。当我关闭并重新打开应用程序时,会反映此问题。我只能保存一次,然后就不再保存了。当我第一次保存时,layout 这里的uri 不是null:
但是当我关闭应用程序并重新启动时,我无法保存并且uri 是null
为什么Uri 变成null?我在代码中写错了什么?从已经非常感谢你了
更新
我发现我的代码的问题是我删除了保存的文件。那是文件被保存的时候,所以我要在你的相应位置搜索文件并删除它,然后我要去我的应用程序,我试图再次保存它,这是 uri 为空的时候。我该如何解决这个问题?
【问题讨论】: