• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps saving the textfile to raw folder?

hi i need some help on my code. how do i save the textfile from a website to the raw folder. did some research but cant seem to find the solution to it :(. can any kind souls please help me thanks alot.

my code
Code:
package com.exercise.AndroidInternetTxt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidInternetTxt extends Activity {
    
    TextView textMsg, textPrompt;
    final String textSource = "http://sites.google.com/site/androidersite/text.txt";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textPrompt = (TextView)findViewById(R.id.textprompt);
        textMsg = (TextView)findViewById(R.id.textmsg);
        
        textPrompt.setText("Wait...");
        
        URL textUrl;
        try {
            textUrl = new URL(textSource);
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
            String StringBuffer;
            String stringText = "";
            while ((StringBuffer = bufferReader.readLine()) != null) {
                stringText += StringBuffer;
            }
            bufferReader.close();
            textMsg.setText(stringText);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        }
        
        textPrompt.setText("Finished!");

    }
}
 
^ that you can do, but that's not the same as the raw folder.
hmmm so it can be done that way? yea. meaning if i update the file in database it will run the database file from the data/data/com.yourpackage/database instead of the file from the raw folder where it first run?

wonder if u could help me with it. i have been searching high and low in the internet but i jux cant find the solution to solve it :(:(
 
Upvote 0
There are some tutorials on the web for copying a DB from assets to the app's internal data directory -- I haven't done this myself but it seems many have.

Try googling "android sqlite assets copy" -- that should turn up one of the tutorials I think.

Hope that helps

oh so this is the keyword to google. dam. google for few days and cant get anything. thanks alot for the key. i try and get back to you here thanks. :D
 
Upvote 0
I should clarify something:

When an app is installed, anything in the assets folder is moved to the data/data/com/whatever folder. This is how APK installation works. With that said, once the app is installed, the data directory cannot be modified as the user is only given READ permissions to that directory and all of its sub directories. The exception to this is if the device is rooted, the permissions to that directory can be changed.
 
Upvote 0
I should clarify something:

When an app is installed, anything in the assets folder is moved to the data/data/com/whatever folder. This is how APK installation works. With that said, once the app is installed, the data directory cannot be modified as the user is only given READ permissions to that directory and all of its sub directories. The exception to this is if the device is rooted, the permissions to that directory can be changed.

i dun kinda understand?
meaning if i wanna do something to change files in data/data/com/whatever folder i mus change the permissions before files in it can be changed?

i still cant find solutions to downloading the a file from webhost and store it in the data/data/com/whatever folder. any kind souls can help :(:(
 
Upvote 0
You shouldn't really be worrying about the folder paths, but instead be using getFilesDir(); and openFileInput(); etc....


See here:

Data Storage | Android Developers

Context | Android Developers


hope that helps


i discovered this code wonder can it be used? i have no error when compilling but crashed when run.

Code:
package com.databasetesting;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DatabasetestingActivity extends Activity {

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private Button startBtn;
    private ProgressDialog mProgressDialog;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startBtn = (Button)findViewById(R.id.startBtn);
        startBtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                startDownload();
            }
        });
    }

    private void startDownload() {
        String url = "http://localhost/abc.txt";
        new DownloadFileAsync().execute(url);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Downloading file..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
        }
    }
    class DownloadFileAsync extends AsyncTask<String, String, String> {
       
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected String doInBackground(String... aurl) {
            int count;

            try {
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/data/data/com.databasetesting/databases");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""+(int)((total*100)/lenghtOfFile));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {}
            return null;

        }
        protected void onProgressUpdate(String... progress) {
             Log.d("ANDRO_ASYNC",progress[0]);
             mProgressDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }
    }
}
 
Upvote 0
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones