How to Edit Read Only File in Android
Android external storage can be used to write and save data, read configuration files etc. This article is continuation of the Android Internal Storage tutorial in the series of tutorials on structured data storage in android.
Android External Storage
External storage such as SD bill of fare can also store application data, at that place's no security enforced upon files you relieve to the external storage.
In full general there are two types of External Storage:
- Primary External Storage: In built shared storage which is "attainable by the user by plugging in a USB cable and mounting it as a drive on a host computer". Example: When we say Nexus 5 32 GB.
- Secondary External Storage: Removable storage. Example: SD Card
All applications can read and write files placed on the external storage and the user can remove them. We demand to bank check if the SD card is bachelor and if we can write to information technology. One time we've checked that the external storage is available simply then we can write to it else the save button would be disabled.
Android External Storage Instance Project Structure
Firstly, we need to make sure that the application has permission to read and write data to the users SD carte du jour, so lets open up up the AndroidManifest.xml
and add the post-obit permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:proper noun="android.permission.READ_EXTERNAL_STORAGE"/>
Also, external storage may be tied up by the user having mounted it every bit a USB storage device. So nosotros need to check if the external storage is available and is not read only.
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { saveButton.setEnabled(false); } private static boolean isExternalStorageReadOnly() { String extStorageState = Surroundings.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { return true; } return false; } private static boolean isExternalStorageAvailable() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { return true; } return false; }
getExternalStorageState()
is a static method of Environment
to make up one's mind if external storage is presently available or not. As y'all can meet if the condition is fake we've disabled the save button.
Android External Storage Example Code
The activity_main.xml layout is defined equally follows:
<?xml version="one.0" encoding="utf-viii"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Reading and Writing to External Storage" android:textSize="24sp"/> <EditText android:id="@+id/myInputText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="x" android:lines="5" android:minLines="3" android:gravity="top|left" android:inputType="textMultiLine"> <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="i.0" android:layout_marginTop="20dp"> <Button android:id="@+id/saveExternalStorage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SAVE" android:layout_weight="0.5"/> <Button android:id="@+id/getExternalStorage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.v" android:text="READ" /> </LinearLayout> <TextView android:id="@+id/response" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
Here apart from the save and read from external storage buttons we display the response of saving/reading to/from an external storage in a textview unlike in the previous tutorial where android toast was displayed.
The MainActivity.java class is given below:
package com.journaldev.externalstorage; import coffee.io.BufferedReader; import coffee.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.bone.Bundle; import android.app.Activity; import android.bone.Surroundings; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public grade MainActivity extends Activity { EditText inputText; TextView response; Button saveButton,readButton; private Cord filename = "SampleFile.txt"; individual Cord filepath = "MyFileStorage"; File myExternalFile; String myData = ""; @Override protected void onCreate(Parcel savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inputText = (EditText) findViewById(R.id.myInputText); response = (TextView) findViewById(R.id.response); saveButton = (Button) findViewById(R.id.saveExternalStorage); saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { attempt { FileOutputStream fos = new FileOutputStream(myExternalFile); fos.write(inputText.getText().toString().getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } inputText.setText(""); response.setText("SampleFile.txt saved to External Storage..."); } }); readButton = (Push button) findViewById(R.id.getExternalStorage); readButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { FileInputStream fis = new FileInputStream(myExternalFile); DataInputStream in = new DataInputStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(in)); Cord strLine; while ((strLine = br.readLine()) != null) { myData = myData + strLine; } in.close(); } catch (IOException e) { e.printStackTrace(); } inputText.setText(myData); response.setText("SampleFile.txt data retrieved from Internal Storage..."); } }); if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { saveButton.setEnabled(false); } else { myExternalFile = new File(getExternalFilesDir(filepath), filename); } } individual static boolean isExternalStorageReadOnly() { Cord extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { render truthful; } return false; } individual static boolean isExternalStorageAvailable() { Cord extStorageState = Environs.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { return true; } return false; } }
- Surround.getExternalStorageState(): returns path to internal SD mount betoken like "/mnt/sdcard"
- getExternalFilesDir(): It returns the path to files folder within Android/data/data/application_package/ on the SD card. It is used to store whatever required files for your app (similar images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.
Likewise if the external storage is not bachelor we disable the save button using the if condition that was discussed before in this tutorial.
Beneath is our application running in android emulator, where nosotros are writing data to file and so reading information technology.
Notation: Make certain your Android Emulator is configured such that it has a SD card every bit shown in the image dialog from AVD beneath. Go to Tools->Android->Android Virtual Device, edit configurations->Show Advance Settings.
This brings an end to this tutorial. We'll discuss storage using Shared Preferences in the adjacent tutorial. Yous can download the terminal Android External Storage Projection from the below link.
smithequescam1964.blogspot.com
Source: https://www.journaldev.com/9400/android-external-storage-read-write-save-file
0 Response to "How to Edit Read Only File in Android"
Postar um comentário