簡體   English   中英

從服務器下載文件並將其放入/ raw文件夾

[英]Downloading files from a server and put it to /raw folder

這是我想做的。 我想制作一個應用程序,例如,它具有一個按鈕,該按鈕將下載某個視頻文件並將其放在resource(raw)文件夾中。 可能嗎?

簡短的回答:您不能

在任何情況下,您都無法在運行時將文件寫入/轉儲到raw / assets文件夾。

您可以下載視頻並將其存儲到內部存儲器 (應用程序保留的存儲器) 或外部存儲器 (通常是SD卡)中

例如,您可以像這樣將媒體文件(例如位圖)存儲到外部存儲中。

 private void saveAnImageToExternalMemory(Bitmap finalBitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    String fname = "yourimagename.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

同樣,從外部存儲器讀取文件,在本示例中為圖像(然后將其加載到imageView)

private void loadImageFromStorage(String path){
    try {
        File f=new File(path, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img=(ImageView)findViewById(R.id.imgPicker);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

編輯 :此外,您可以將數據存儲到內部存儲器中

或者,您也可以將位圖保存到內部存儲器中,以防SD卡不可用或出於其他任何原因。 保存到內部存儲器的文件只能由保存文件的應用程序訪問。 用戶和其他應用程序都無法訪問這些文件

public boolean saveImageToInternalStorage(Bitmap image) {
    try {
        FileOutputStream fos = context.openFileOutput("yourimage.png", Context.MODE_PRIVATE);
        // Writing the bitmap to the output stream
        image.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
        return true;
    } catch (Exception e) {
        Log.e("saveToInternalStorage()", e.getMessage());
        return false;
    }
}

查看此文檔以獲取更多信息

問候,

暫無
暫無

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

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