Android Download File From URL And Save To Internal Storage
Last Updated : Jul 1, 2023
IN - Android | Written & Updated By - Ashish
In this article we will show you the solution of android download file from url and save to internal storage and we'll examine how to save and read data into files utilising the internal storage of an Android device.
Android internal storage refers to the memory of the device where personal information is kept. By design, the internal storage is private to the application when saving and loading files, meaning that other applications cannot access these files.
Step By Step Guide On Android Download File From URL And Save To Internal Storage :-
The internal stored files linked to the application are also deleted when the user uninstalls the application.
But keep in mind that some Android users have rooted their devices and obtained superuser access. These users will have access to any files they want to view and write.
To alter reading and writing streams from and to local files, Android provides openFileInput and openFileOutput from the Java I/O classes.
The method openFileOutput() returns a FileOutputStream instance. FileOutputStream fOut = openFileOutput("file name",Context.MODE PRIVATE); The write method can then be used to write data to the file.
Then we call the read method to read the file one character at a time and print it. The syntax is as follows:
temp = temp + Character.toString((char)c); while ((c = fin.read())!= -1) int c; String temp="";
fin.close(); In the code above, string temp has all of the file's data.
Keep in mind that these techniques only accept basic file names and do not support file paths (such as path/to/file.txt).
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.internalstoragedemo.MainActivity"> <TextView android:text="@string/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="51dp" android:layout_marginStart="51dp" android:layout_marginTop="59dp" android:id="@+id/txtname" android:textStyle="bold|italic" android:textSize="18sp" /> <TextView android:text="@string/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtname" android:layout_alignLeft="@+id/txtname" android:layout_alignStart="@+id/txtname" android:layout_marginTop="56dp" android:id="@+id/txtpass" android:textStyle="bold|italic" android:textSize="18sp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="8" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/txtpass" android:layout_toEndOf="@+id/txtpass" android:layout_marginLeft="21dp" android:layout_marginStart="21dp" android:layout_marginTop="48dp" android:id="@+id/editName" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:layout_below="@+id/editName" android:layout_alignLeft="@+id/editName" android:layout_alignStart="@+id/editName" android:layout_marginTop="35dp" android:id="@+id/editPass" /> <Button android:text="@string/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editPass" android:layout_alignLeft="@+id/txtpass" android:layout_alignStart="@+id/txtpass" android:layout_marginTop="86dp" android:id="@+id/button" android:onClick="save"/> // OnClick "save" <Button android:text="@string/next" android:la android:layout_height="wrap_content" android:layout_alignTop="@+id/button" android:layout_alignRight="@+id/editName" android:layout_alignEnd="@+id/editName" android:layout_marginRight="25dp" android:layout_marginEnd="25dp" android:id="@+id/button2" android:onClick="next"/> // OnClick "next" </RelativeLayout> import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { EditText editname,editpass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editname = (EditText) findViewById(R.id.editName); editpass= (EditText) findViewById(R.id.editPass); } public void save(View view) // SAVE { File file= null; String name = editname.getText().toString(); String password = editpass.getText().toString(); FileOutputStream fileOutputStream = null; try { name = name + " "; file = getFilesDir(); fileOutputStream = openFileOutput("Code.txt", Context.MODE_PRIVATE); //MODE PRIVATE fileOutputStream.write(name.getBytes()); fileOutputStream.write(password.getBytes()); Toast.makeText(this, "Saved \n" + "Path --" + file + "\tCode.txt", Toast.LENGTH_SHORT).show(); editname.setText(""); editpass.setText(""); return; } catch (Exception ex) { ex.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public void next( View view) //NEXT { Toast.makeText(this,"NEXT", Toast.LENGTH_SHORT).show(); Intent intent= new Intent(this, Main2Activity.class); startActivity(intent); } }
- Name your new project InternalStorageDemo when you create it.
- To add the following code, open res -> layout -> activity main.xml (or) main.xml.
- In this step, we enter MainActivity and add the save and next functions that were defined over the button onclick. The save function takes the information from edittext and stores it within a file in byte format. Here, Toast was also used to display the location of the file along with its name. The following function uses intent to advance to the subsequent action linked to it.
Conclusion :-
This is how we can implement the URL and save it to internal storage.
I hope this article on android download file from url and save to internal storage helps you and the steps and method mentioned above are easy to follow and implement.
package