簡體   English   中英

Android,如何在內部存儲中存儲圖像?

[英]Android, How to store image in internal storage?

我想在內部存儲(而不是外部存儲)上存儲位圖圖像。 我寫了這段代碼,但似乎有問題。 因為當我從DDMS下載圖像時,我無法打開它。

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {

        String fileName = Long.toString(System.currentTimeMillis()) + ".png";

        try {
            OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE));
            osw.write(outputImage.toString());
            Log.i(TAG, "Image stored at: " + fileName);
        } catch (Exception e) {
            Log.w(TAG, e.toString()); 
            fileName = null;
        } 

        return fileName;
    } 

outputImage.toString()不是圖像:)你放在文件上的不是二進制數據,而是一些字符串!

一種方法是這樣的:

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
    String fileName = Long.toString(System.currentTimeMillis()) + ".png";

    final FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
    outputImage.compress(CompressFormat.PNG, 90, fos);
}

我直接編碼到瀏覽器中,可能會有一些語法錯誤,但代碼應該可以工作。

問題是您使用.toString()而不是將Bitmap壓縮為FileOutputStream:

FileOutputStream out = new FileOutputStream(filename);
outputImage.compress(Bitmap.CompressFormat.PNG, 90, out);

內部存儲也可以通過Context檢索。

File cacheDir = context.getCacheDir();

暫無
暫無

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

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