繁体   English   中英

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

[英]How do I save images in storage particular folder (gallery) & Get image from storage in above android 11 or below with java?

就像在我的绘画艺术项目中一样,我希望使用 Android Java 在我的应用程序 Recycler 视图中保存绘制的图像以存储或检索相同的图像,我从 android 11 以下而不是 11 以上的图像获取并保存图像我该怎么办?

如果得到某人的帮助并且如果我得到解决方案而不是肯定在我的答案中提出......谢谢你

Hi First add below line to manifest in application tab

// for make directory in android 10 & solve error in android 8 
<application
android:requestLegacyExternalStorage="true"
android:hardwareAccelerated="false"
android:largeHeap="true"

& than try below code / method

// in blank space set folder name of your wish and put this in res - values - string
<resources>
<string name="app_folder_name">________________</string>

 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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM