簡體   English   中英

如何在Android Kitkat中訪問(讀取/寫入)外部sdcard中的文件?

[英]How can I access(read/write) file in the external sdcard in Android Kitkat?

這是我的源代碼; 我已經在external_SD上安裝了microsdsvc應用。

szSDCardFileName = "/storage/external_SD/Android/data/com.tmnt.microsdsvc/files/AAA.DAT";

if ((fp = open(szSDCardFileName, O_RDWR|O_DIRECT|O_SYNC, S_IRWXU)) == -1) {
    if ((fp = open(szSDCardFileName, O_RDWR|O_DIRECT|O_SYNC|O_CREAT, S_IRWXU)) == -1) {
        return -1;
    }
}

memset(g_buf, 0x00, BLOCK_LENGTH);
if (read(fp, (char *)g_buf, BLOCK_LENGTH) == ERROR) {
    __android_log_print(ANDROID_LOG_INFO, "JNI", "<Err>file read error[errno=%d, handle=%d]", errno, fp);
    return -1;
}

在Lg G2 Kitkat版本上運行代碼時,open()正常,但是下一個read()失敗,錯誤號為22。

我不知道我錯了!

也許這就是為什么...

KitKat應用程序將不再能夠在帶有內部閃存和可移動/外部SD卡的“二級外部存儲設備”或雙存儲設備上分別創建,修改或刪除文件和文件夾。

“ WRITE_EXTERNAL_STORAGE權限只能授予對設備上主要外部存儲設備的寫訪問權限。除綜合權限允許的應用程序包特定目錄中的應用程序外,不得將應用程序寫入輔助外部存儲設備。”

http://source.android.com/devices/tech/storage/index.html

package com.example.readfilefromexternalresource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {
    private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView)findViewById(R.id.textView);
    String state = Environment.getExternalStorageState();

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

        Toast.makeText(this, "There is no any sd card", Toast.LENGTH_LONG).show();


    } else {
        BufferedReader reader = null;
        try {
            Toast.makeText(this, "Sd card available", Toast.LENGTH_LONG).show();
            File file = Environment.getExternalStorageDirectory();
            File file2 = new File(file.getAbsolutePath()+File.separator + "myText.txt");
            reader = new BufferedReader(new FileReader(file2));
            StringBuilder textBuilder = new StringBuilder();
            String line;
            while((line = reader.readLine()) != null) {
                textBuilder.append(line);
                textBuilder.append("\n");

            }
            textView.setText(textBuilder);

        } catch (FileNotFoundException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
}

}

  • 首先,您需要獲取外部SD卡的狀態
  • 然后檢查是否有已安裝的sd卡,如果未找到sd卡,則它什么也不做(您可以在此處輸入錯誤消息)
  • 獲取其絕對路徑並添加分隔符,然后放入要讀取的文件的文件名
  • 使用BufferedReader讀取您的textFile。
  • 使用StringBuilder來構建您的字符串,它比簡單的疊加字符串更有效
  • 附加字符串后,請始終關閉閱讀器以節省內存,眾所周知,智能手機的內存少於台式機
  • 調用StringBuiler的toString()方法創建您可以使用的String

     String state = Environment.getExternalStorageState(); if(!state.equals(Environment.MEDIA_MOUNTED)) { } else { File externalDir = Environment.getExternalStorageDirectory(); File textFile = new File(externalDir.getAbsolutePath() + File.separator + fileName); BufferedReader reader = new BufferedReader(new FileReader(textFile)); StringBuilder textBuilder = new StringBuilder(); String line; while((line = reader.readLine()) != null) { textBuilder.append(line); textBuilder.append("\\n"); } reader.close(); String yourString = textBuilder.toString(); } 

它會引發FileNotFound異常。 您可以在寫入文件時執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM