在我的绘画艺术项目中,我希望将绘制的图像保存或检索到我的应用程序回收视图中,使用Android &我从android 11下面获得和保存图像,而不是从11上面保存图像,我该怎么办?
感谢如果我得到了别人的帮助&如果我得到了比确定的答案更好的答案.谢谢你
发布于 2022-07-14 05:36:09
首先,请在“应用程序”选项卡中添加下面的“清单”
用于android 10中的make目录&解决android 8中的错误
<application
android:requestLegacyExternalStorage="true"
android:hardwareAccelerated="false"
android:largeHeap="true" />&不要尝试下面的代码/方法
在空白处设置文件夹名称,并将其放入res - values string中。
<resources>
<string name="app_folder_name">Your_Folder_name</string>
</resources>然后将此方法添加到您的活动中。
private void saveToGallery() {
String parentPath="";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// for above android 11
parentPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + getString(R.string.app_folder_name);
}else{
// for below android 11
parentPath = Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_folder_name);
}
File parentFile = new File(parentPath);
Log.e("TAG", "saveToGallery1: "+parentFile);
if (!parentFile.exists()){
parentFile.mkdirs();
}
File imageFile = new File(parentFile, "drawing"+System.currentTimeMillis() + ".png"); // Imagename.png
FileOutputStream out = null;
try {
out = new FileOutputStream(imageFile);
Bitmap bmp = binding.paintView.save();
Common.DRAWING_BITMAP = bmp;
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();
// Tell the media scanner about the new file so that it is // immediately available to the user.
MediaScannerConnection.scanFile(PaintDrawActivity.this, new String[]{imageFile.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage1", "Scanned " + path + ":");
Log.e("ExternalStorage1", "-> uri=" + uri);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void getMyWorkImagesFromStorage() {
File file;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// for above android 11
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), Environment.DIRECTORY_PICTURES + File.separator + getString(R.string.app_folder_name));
} else {
// for below android 11
file = new File(Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_folder_name));
}
File[] files = file.listFiles();
if (files != null) {
for (File file1 : files) {
if (file1.getPath().endsWith(".png") || file1.getPath().endsWith(".jpg")) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file1.getAbsolutePath(), bmOptions);
myWorkItems.add(new MyGalleryItem(bitmap, file1.getName()));
Common.MY_GALLERY_IMAGES = myWorkItems;
}
}
if (files.length == 0) {
binding.tvEmpty.setVisibility(View.VISIBLE);
} else {
binding.tvEmpty.setVisibility(View.GONE);
}
}
}https://stackoverflow.com/questions/72949856
复制相似问题