首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在存储特定文件夹(图库)中保存图像&使用java从android 11或以下的存储中获取图像?

如何在存储特定文件夹(图库)中保存图像&使用java从android 11或以下的存储中获取图像?
EN

Stack Overflow用户
提问于 2022-07-12 09:15:23
回答 1查看 103关注 0票数 1

在我的绘画艺术项目中,我希望将绘制的图像保存或检索到我的应用程序回收视图中,使用Android &我从android 11下面获得和保存图像,而不是从11上面保存图像,我该怎么办?

感谢如果我得到了别人的帮助&如果我得到了比确定的答案更好的答案.谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-14 05:36:09

首先,请在“应用程序”选项卡中添加下面的“清单”

用于android 10中的make目录&解决android 8中的错误

代码语言:javascript
运行
复制
<application
     android:requestLegacyExternalStorage="true"
     android:hardwareAccelerated="false"
     android:largeHeap="true" />

&不要尝试下面的代码/方法

在空白处设置文件夹名称,并将其放入res - values string中。

代码语言:javascript
运行
复制
<resources>
     <string name="app_folder_name">Your_Folder_name</string>
</resources>

然后将此方法添加到您的活动中。

代码语言:javascript
运行
复制
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);
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72949856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档