日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發(fā)布時間:2023/12/15 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 存储无法写入,在Android中的外部存储中写入文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我想在外部存儲sdCard中創(chuàng)建一個文件并寫入它.我已經(jīng)通過互聯(lián)網(wǎng)搜索并嘗試但沒有得到結(jié)果,我已經(jīng)在Android Manifest文件中添加了權(quán)限,我在模擬器上這樣做,我正在嘗試下面的代碼和獲取ERRR“,”無法創(chuàng)建文件“.

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

解決方法:

您也可以使用此代碼執(zhí)行此操作.

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

總結(jié)

以上是生活随笔為你收集整理的android 存储无法写入,在Android中的外部存储中写入文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。