簡體   English   中英

如何在 android studio(JAVA) 中壓縮圖像並將其存儲在設備特定文件夾中

[英]How do I compress image in android studio(JAVA) and store it in device specific folder

我正在制作一個用於圖像壓縮的應用程序而且我是 Android 開發的新手......所以我的問題是我們在訪問 JAVA 中的外部存儲時遇到了一些問題......我試過了

compile 'id.zelory:compressor:2.1.0'
compressedImage = new Compressor(this)
    .setMaxWidth(640)
    .setMaxHeight(480)
    .setQuality(75)
    .setCompressFormat(Bitmap.CompressFormat.WEBP)
    .setDestinationDirectoryPath(
        Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES
        ).getAbsolutePath()
    )
    .compressToFile(actualImage);

好的...那么我可以使用 WEBP_LOSSLESS 而不是 WEBP 來處理 Android API 30 嗎? 主要問題是如何設置存儲在特定文件夾中的目的地?

getExternalStoragePublicDirectory的紅色棄用框列出了替代方案:

通過遷移到Context#getExternalFilesDir(String)MediaStoreIntent#ACTION_OPEN_DOCUMENT等替代方案,應用程序可以繼續訪問存儲在共享/外部存儲上的內容。

Bitmap.CompressFormat.WEBP還包含一個棄用通知:

此字段在 API 級別 30 中已棄用。有利於更明確的CompressFormat#WEBP_LOSSYCompressFormat#WEBP_LOSSLESS


例如,這樣的事情應該可以工作:

Context ctx = this;
File storeFolder = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

compressedImage = new Compressor(this)
    .setMaxWidth(640)
    .setMaxHeight(480)
    .setQuality(75)
    .setCompressFormat(Bitmap.CompressFormat.WEBP_LOSSLESS)
    .setDestinationDirectoryPath(
        storeFolder.getAbsolutePath()
    )
    .compressToFile(actualImage);

暫無
暫無

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

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