gpt4 book ai didi

java - 需要帮助保存图像文件

转载 作者:行者123 更新时间:2023-12-01 22:09:41 25 4
gpt4 key购买 nike

我正在尝试将签名板图像保存到文件,但我的应用程序似乎没有在 storage/emulated/0 中创建目录

这是我用来创建文件夹的...

    String DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/Signature/";
String pic_name = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
String StoredPath = DIRECTORY + pic_name + ".png";

这就是我用来创建目录的内容

        file = new File(DIRECTORY);
if (!file.exists()) {
file.mkdir();
}

最后,这是文件保存的位置...

        public void save(View v, String StoredPath) {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (bitmap == null) {
bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(bitmap);
try {
// Output the file
FileOutputStream mFileOutStream = new FileOutputStream(StoredPath);
v.draw(canvas);

// Convert the output file to Image such as .png
bitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();

} catch (Exception e) {
Log.v("log_tag", e.toString());
}

}

请注意,这不是我自己的代码...(基本上是复制粘贴的) http://demonuts.com/android-capture-signature-using-canvas/

本本工作正常,我可以在上面写字,但无法保存:(当我将其发送到 Toast 时,我可以返回 Storage/emulated/0/Signature/20191101_a time stamp.png 的完整目录和文件名,但它不会保存在设备上。

有什么想法吗?

最佳答案

在AndroidManifest.xml文件中声明权限,授予外部存储读写权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

对于 Android 操作系统版本 > 6.0,您还需要运行时权限

// Check whether this app has write external storage permission or not.
int permission = ContextCompat.checkSelfPermission(ExternalStorageActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

// If do not grant write external storage permission.
if(permission!= PackageManager.PERMISSION_GRANTED)
{
// Request user to grant write external storage permission.
ActivityCompat.requestPermissions(ExternalStorageActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION);
}

关于java - 需要帮助保存图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com