簡體   English   中英

在ANdroid中讀取和寫入圖像到SDCard

[英]Read and write an image to SDCard in ANdroid

我正在嘗試從URL讀取文件並將其寫入sdcard。 因為它可以與其他文件(例如.js .css和.txt文件等)配合使用,但是...不適用於.png .jpg .bmp和.gif。 我的代碼是

String path = dir + value;
            File localConfigfile = new File(
                    Environment.getExternalStorageDirectory()
                            + "/Avalon/assets/www/" + path);
            String responseString = RequestMgr.GetWebSource(DomainName
                    + "/" + Heirarchy + "/" + Serverpath);
            FileOutputStream out;
if (localConfigfile.exists()) {
                    localConfigfile.delete();
                    out = new FileOutputStream(localConfigfile);
                    out.write(responseString.getBytes());
                }
            }

請在這里幫助我,我為此感到震驚

嘗試使用位圖讀取文件。 如果您使用位圖和URI進行讀取,這將是一個不錯的選擇

private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
    Log.d(TAG,
            "Error creating media file, check storage permissions: ");// e.getMessage());
    return;
} 
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
} catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
} } 

暫無
暫無

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

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