android 存储无法写入,在Android中的外部存储中写入文件

我想在外部存储sdCard中创建一个文件并写入它.我已经通过互联网搜索并尝试但没有得到结果,我已经在Android Manifest文件中添加了权限,我在模拟器上这样做,我正在尝试下面的代码和获取ERRR“,”无法创建文件“.

btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);

btnWriteSDFile.setOnClickListener(new OnClickListener() {

//private Throwable e;

@Override

public void onClick(View v) {

// write on SD card file data from the text box

try {

File myFile = new File("/sdcard/mysdfile.txt");

myFile.createNewFile();

FileOutputStream fOut = new FileOutputStream(myFile);

OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

myOutWriter.append(txtData.getText());

myOutWriter.close();

fOut.close();

} catch (Exception e) {

Log.e("ERRR", "Could not create file",e);

}

}// onClick

}); // btnWriteSDFile

解决方法:

您也可以使用此代码执行此操作.

public class WriteSDCard extends Activity {

private static final String TAG = "MEDIA";

private TextView tv;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv = (TextView) findViewById(R.id.TextView01);

checkExternalMedia();

writeToSDFile();

readRaw();

}

/** Method to check whether external media available and writable. This is adapted from

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal */

private void checkExternalMedia(){

boolean mExternalStorageAvailable = false;

boolean mExternalStorageWriteable = false;

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

// Can read and write the media

mExternalStorageAvailable = mExternalStorageWriteable = true;

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

// Can only read the media

mExternalStorageAvailable = true;

mExternalStorageWriteable = false;

} else {

// Can't read or write

mExternalStorageAvailable = mExternalStorageWriteable = false;

}

tv.append("\n\nExternal Media: readable="

+mExternalStorageAvailable+" writable="+mExternalStorageWriteable);

}

/** Method to write ascii text characters to file on SD card. Note that you must add a

WRITE_EXTERNAL_STORAGE permission to the manifest file or this method will throw

a FileNotFound Exception because you won't have write permission. */

private void writeToSDFile(){

// Find the root of the external storage.

// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal

File root = android.os.Environment.getExternalStorageDirectory();

tv.append("\nExternal file system root: "+root);

// See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

File dir = new File (root.getAbsolutePath() + "/download");

dir.mkdirs();

File file = new File(dir, "myData.txt");

try {

FileOutputStream f = new FileOutputStream(file);

PrintWriter pw = new PrintWriter(f);

pw.println("Hi , How are you");

pw.println("Hello");

pw.flush();

pw.close();

f.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

Log.i(TAG, "******* File not found. Did you" +

" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");

} catch (IOException e) {

e.printStackTrace();

}

tv.append("\n\nFile written to "+file);

}

/** Method to read in a text file placed in the res/raw directory of the application. The

method reads in all lines of the file sequentially. */

private void readRaw(){

tv.append("\nData read from res/raw/textfile.txt:");

InputStream is = this.getResources().openRawResource(R.raw.textfile);

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer size

// More efficient (less readable) implementation of above is the composite expression

/*BufferedReader br = new BufferedReader(new InputStreamReader(

this.getResources().openRawResource(R.raw.textfile)), 8192);*/

try {

String test;

while (true){

test = br.readLine();

// readLine() returns null if no more lines in the file

if(test == null) break;

tv.append("\n"+" "+test);

}

isr.close();

is.close();

br.close();

} catch (IOException e) {

e.printStackTrace();

}

tv.append("\n\nThat is all");

}

}

标签:android,android-layout,android-emulator

来源: https://codeday.me/bug/20190911/1804705.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值