我正在尝试将位图保存到文件中,在此期间在 Android Studio 的 QRGSaver 类中的 outStream = new FileOutputStream(imageDetail) 行出现以下错误
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/QRCode/5183942.jpg: open
failed:ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
at com.example.qrcodegenerateandsaveex.qrgenerator.QRGSaver.save(QRGSaver.java:37)
at com.example.qrcodegenerateandsaveex.MainActivity$2.onClick(MainActivity.java:98)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
W/System.err: at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
at libcore.io.IoBridge.open(IoBridge.java:478)
... 16 more
我还在 AndroidManifest.xml 文件中添加了 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 权限。
我花了几个小时但没有成功。有人可以帮我吗?
private String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/";
String imageName = "image1324";
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 0:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
try {
boolean save = new QRGSaver().save(savePath, imageName, bitmap,
QRGContents.ImageType.IMAGE_JPEG);
String result = save ? "Image Saved" : "Image Not Saved";
Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
edtValue.setText(null);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
下面是QRGSaver类
public class QRGSaver {
public boolean save(String saveLocation, String imageName, Bitmap bitmap, int imageFormat) {
boolean success = false;
String imageDetail = saveLocation + imageName + imgFormat(imageFormat);
FileOutputStream outStream;
File file = new File(saveLocation);
if (!file.exists()) {
file.mkdir();
} else {
Log.d("QRGSaver", "Folder Exists");
}
try {
outStream = new FileOutputStream(imageDetail);
bitmap.compress((Bitmap.CompressFormat) compressFormat(imageFormat), 100, outStream);
outStream.flush();
outStream.close();
success = true;
} catch (IOException e) {
Log.d("QRGSaver", e.toString());
e.printStackTrace();
}
return success;
}
public boolean save(String saveLocation, String imageName, Bitmap bitmap) {
return save(saveLocation, imageName, bitmap, QRGContents.ImageType.IMAGE_PNG);
}
private String imgFormat(int imageFormat) {
return imageFormat == QRGContents.ImageType.IMAGE_PNG ? ".png" : ".jpg";
}
private Comparable<? extends Comparable<? extends Comparable<?>>> compressFormat(int imageFormat)
{
return imageFormat == QRGContents.ImageType.IMAGE_PNG ? Bitmap.CompressFormat.PNG :
(imageFormat == QRGContents.ImageType.IMAGE_WEBP ? Bitmap.CompressFormat.WEBP :
Bitmap.CompressFormat.JPEG);
}
}