Post by Admin on May 14, 2017 16:21:07 GMT 7
File
Một trong những thứ không thể thiếu trong viết ứng dụng là hoạt động với file, chúng ta có thể tạo ra những dữ liệu mới, hoặc lưu lại những thứ ta vừa mới làm việc xong.
Có điều có 1 vấn đề đó là việc truy cập file thường được cho là nguy hiểm, mặc dù vậy, nó vẫn không ngăn được việc người dùng vô tội bị đánh cắp dữ liệu, hoặc thay đổi thuộc tính file, cài phần mềm độc hại….
Ta sẽ nói đến file androidmanifest.xml và thuộc tính của nó
ở trên là file AndroidMaifest.xml mà tôi đã chỉnh sửa để có quyền truy cập đến file, chúng ta thấy đó được thêm vào dòng sau:
Đó là quyền đọc và viết lên bộ nhớ ngoài, bạn đừng tưởng rằng bộ nhớ ngoài là thể SD của bạn, ngay cả khi không có thẻ nhớ, bộ nhớ ngoài của bạn đã bao gồm 1 vùng được android mặc định coi là bộ nhớ ngoài (sdcard0)
Sau khi thêm 2 dòng đó vào file androidmanifest.xml là coi như xong, việc đọc viết dữ liệu trên file là khá đơn giản.
Một trong những thứ không thể thiếu trong viết ứng dụng là hoạt động với file, chúng ta có thể tạo ra những dữ liệu mới, hoặc lưu lại những thứ ta vừa mới làm việc xong.
Có điều có 1 vấn đề đó là việc truy cập file thường được cho là nguy hiểm, mặc dù vậy, nó vẫn không ngăn được việc người dùng vô tội bị đánh cắp dữ liệu, hoặc thay đổi thuộc tính file, cài phần mềm độc hại….
Ta sẽ nói đến file androidmanifest.xml và thuộc tính của nó
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.holyeyed.ctrfile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ở trên là file AndroidMaifest.xml mà tôi đã chỉnh sửa để có quyền truy cập đến file, chúng ta thấy đó được thêm vào dòng sau:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Đó là quyền đọc và viết lên bộ nhớ ngoài, bạn đừng tưởng rằng bộ nhớ ngoài là thể SD của bạn, ngay cả khi không có thẻ nhớ, bộ nhớ ngoài của bạn đã bao gồm 1 vùng được android mặc định coi là bộ nhớ ngoài (sdcard0)
Sau khi thêm 2 dòng đó vào file androidmanifest.xml là coi như xong, việc đọc viết dữ liệu trên file là khá đơn giản.
package com.holyeyed.ctrfile;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
EditText ed;
Button save;
String path = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// lấy một đường dẫn để đọc và viết file
path = Environment.getExternalStorageDirectory() + "/a.txt";
// tạo thành phần giao diện
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ed = new EditText(this);
// lấy dữ liệu từ file
ed.setText(read(path));
save = new Button(this);
save.setText("SAve");
save.setOnClickListener(this);
ll.addView(ed);
ll.addView(save);
// cho hiện thị ra ngoài
setContentView(ll);
}
public void write(String fn, byte[] data) {
try {
// khởi tạo file từ đường dẫn
File f = new File(fn);
// mở chuỗi viết file
FileOutputStream fos = new FileOutputStream(f);
// viết dữ liệu vào
fos.write(data);
} catch (Exception e) {
}
}
public String read(String fn) {
try {
// chuỗi kết quả
String ketqua = "";
// mở file từ đường dẫn
File f = new File(fn);
// mở chuỗi đọc file
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
// đọc từng dòng cho tới hết
while (dis.available() > 0)
ketqua += dis.readLine();
// trả lại kết quả
return ketqua;
} catch (Exception e) {
}
return "can not read";
}
public void onClick(View arg0) {
try {
// lấy dữ liệu từ khung nhập
String str = ed.getText() + "";
// thực hiện hành động lưu file
write(path, str.getBytes("utf-8"));
} catch (Exception e) {
}
}
}