Java Android 临时保存位图图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22781430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:50:06  来源:igfitidea点击:

Android saving bitmap image temporarily

javaandroidfilebitmap

提问by Mohitt

I am looking out for a way to save a bitmap file temporarily in android file system. The file is required only until it is used as a part of POST request to a server after which I want it to cease to exist. I am looking for the faster way of doing this.

我正在寻找一种在 android 文件系统中临时保存位图文件的方法。该文件仅在它被用作对服务器的 POST 请求的一部分之前是必需的,之后我希望它不再存在。我正在寻找更快的方法来做到这一点。

...
File file = new File(Environment.getExternalStorageDirectory().getPath().toString()+"/ImageDB/" + fileName+".png");
FileOutputStream filecon = new FileOutputStream(file);
sampleResized.compress(Bitmap.CompressFormat.JPEG, 90, filecon);
... 

I am currently using this method.

我目前正在使用这种方法。

EDIT: I got my solution from Creating temporary files in Android

编辑:我从在 Android 中创建临时文件得到了我的解决方案

采纳答案by Don Chakkappan

File f3=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
if(!f3.exists())
    f3.mkdirs();        
OutputStream outStream = null;
File file = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"seconds"+".png");
try {
    outStream = new FileOutputStream(file);
    mBitmap.compress(Bitmap.CompressFormat.PNG, 85, outStream);
    outStream.close();
    Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (Exception e) {
    e.printStackTrace();
} 

回答by Mr.India

You can use file's file.delete()method , after closing filecon

file.delete()关闭后,您可以使用文件的方法filecon

 File file = new File(Environment.getExternalStorageDirectory().getPath().toString()+"/ImageDB/" + fileName+".png");
    FileOutputStream filecon = new FileOutputStream(file);
    sampleResized.compress(Bitmap.CompressFormat.JPEG, 90, filecon);
    if(filecon!null=) filecon.close;
    file.delete();

回答by SteamFire

Get the response of your post and then add this into:

获取您帖子的回复,然后将其添加到:

boolean deleted = file.delete();

boolean deleted = file.delete();

You can get the confirmation of the deletion like this.

您可以像这样获得删除确认。

回答by sharma_kunal

Please check the below code. All the above codes are right.But if we compress JPEG it work fast as compare to PNG. So Better to use JPEG to imporove performance..

                        FileOutputStream fileOutputStream = new FileOutputStream(path);
                        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
                        viewCapture.compress(CompressFormat.JPEG, 50, bos);
                        bos.flush();
                        bos.close();


For Delete just use

    File myFile = new File(path);
    myFile.delete();

Hope its helpfull for you

希望对你有帮助