簡體   English   中英

位圖未保存到內部存儲 (Android)

[英]Bitmaps Not Being Saved to Internal Storage (Android)

問題

我正在使用 Java 在 Android Studio 中創建一個 Android 應用程序。它需要一個 function 來獲取 bitmap 圖像並將其保存到內部 memory 中的文件夾結構中。我正在使用的圖像是 PNG 我當前的實現基於此處找到的答案(Saving and Reading Bitmaps/Images from Internal memory in Android) 我很困惑為什么不是所有的日志語句都被執行並且沒有文件被創建:

    private String saveToInternalStorage( Bitmap bitmapImage, String dataType, int zoom, int x, int y )
    {
        Log.w(TAG, "Function Started.");
        String imageDir = String.format( "%s/%d/%d", dataType, zoom, x );
        String imageName = String.format( "%d.png", y );

        ContextWrapper contextWrapper = new ContextWrapper( getContext() );
        Log.w(TAG, "Passed Context Wrapper.");

        // path to /data/data/app_name/app_data/<dataType>/<zoom>/<x>
        // - - - NO LOGS SHOWN PAST THIS POINT - - -
        File directory = contextWrapper.getDir( imageDir, Context.MODE_PRIVATE );
        Log.w(TAG, "Passed Directory Creation.");

        // Create image at imageDir (<y>.png)
        File imageFile = new File( directory, imageName );
        Log.w(TAG, "Passed File Creation.");

        FileOutputStream outputStream = null;
        Log.e(TAG, "Begining to Output to File.");
        try
        {
            outputStream = new FileOutputStream( imageFile );
            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress( Bitmap.CompressFormat.PNG, 100, outputStream );
        }
        catch( Exception e )
        {
            Log.e(TAG, "Error Adding Image to Internal Storage.");
            e.printStackTrace();
        }
        finally
        {
            try
            {
                outputStream.flush();
                outputStream.close();
            }
            catch( Exception e )
            {
                Log.e(TAG, "Error Closing Output Stream.");
                e.printStackTrace();
            }
        }

        return directory.getAbsolutePath();
    }

以下兩行是 Logcat 中顯示的唯一相關的 output。 沒有顯示任何錯誤,也沒有顯示來自上述 function 的其他日志

2022-02-08 02:52:56.433 18914-18990/com.example.biomapper W/ContentValues: Function Started.
2022-02-08 02:52:56.434 18914-18990/com.example.biomapper W/ContentValues: Passed Context Wrapper.

我試過的

這是我第三次嘗試創建這個 function,也是我最接近讓它工作的一次嘗試。 關於這個主題,我在 Stack Overflow 上查看了許多其他問題。 我添加了許多日志語句以查看哪里/可能出了什么問題。 我試過使用file.exists()命令來測試文件是否被創建過。 我確信在測試時給這個 function 的參數是正確的。

非常感謝任何有關將圖像保存到內部存儲、檢查文件是否存在或以任何方式改進我的代碼的建議。

通過這種方式,我將圖像保存在 android 文件夾中 在 res 文件夾中創建目錄“xml”並粘貼

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.project/files/Pictures" />
</paths>

在清單文件中,在應用程序下添加此行

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.xyz.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_path" />
</provider>

在您各自的活動中初始化它

File photoFile = null;

根據您的要求編輯此 function !!

try {

                    photoFile = createImageFile();
                    displayMessage(getBaseContext(), photoFile.getAbsolutePath());

                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Uri photoURI = FileProvider.getUriForFile(this,"com.xyz.fileprovider",photoFile);
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST);
                    }
                } catch (Exception ex) {
                    // Error occurred while creating the File
                    displayMessage(getBaseContext(), ex.getMessage().toString());
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM