[英]When I try to upload and save an image file using Struts in eclipse it shows the file name as null
[英]Null uri when i delete the file and i try to save it again
我正在嘗試以圖像格式保存屏幕的所有內容,為此,在我的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 獲取 null 的時間。 我該如何解決這個問題?
這個錯誤主要取決於 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.