devRey
devRey

Reputation: 53

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

Here is I want to make. I want to make an app, for example it has a button that will download a certain video file and put it on the resource(raw) folder. Is it possible?

Upvotes: 0

Views: 2938

Answers (2)

Edwin Tigor
Edwin Tigor

Reputation: 7

You can use android sqlite database to manage your download file.

Upvotes: 0

Ricardo Vieira
Ricardo Vieira

Reputation: 1818

Short answer : You can not.

You can not, under any circumstance, write/dump a file to the raw/assets folder in runtime.

What you can do is to download the video and store it into Internal Memory (application reserved storage) or External Memory (usually your SDCard))

For example, you can store media files, for instance a Bitmap to your external storage like this.

 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();
    }
}

And equally, read an file, in this example an image (which is then loaded to an imageView), from external memory

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();
    }

}

Edit: Additionally, you can store your data into internal memory

Alternatively you can also save the Bitmap to the internal storage in case the SD card is not available or for whatever other reasons you may have. Files saved to the internal storage are only accessible by the application which saved the files. Neither the user nor other applications can access those files

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;
    }
}

Check this documentation for more information

Regards,

Upvotes: 1

Related Questions