SORU
25 Mayıs 2010, Salı


Nasıl bir okuma metni Android SD karttan dosya alabilir miyim?

Android geliştirme için yeni duyuyorum.

SD kartından bir metin dosyası okumak ve bir metin dosyası görüntülemek istiyorum. Android bir metin dosyasına doğrudan görüntülemek için herhangi bir yol ya da başka nasıl bir metin dosyasının içeriğini okumak gösterebilir miyim?

CEVAP
25 Mayıs 2010, Salı


In your layout bir metni görüntülemek için ihtiyacınız olacak. TextView bir seçimdi. Böyle bir şey olacak:

<TextView 
    android:id="@ id/text_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/>

Ve kodunuzu bu gibi görünecektir:

//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
    br.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text);

Bu sadece yapmak istediğin şeyin ne olduğunu Activity ya da başka bir yerde bağlı olarak onCreate() yöntemi gidebilir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Carlos Delgado

    Carlos Delga

    21 HAZİRAN 2011
  • Dylan Brenan

    Dylan Brenan

    22 Aralık 2009
  • gsmaestro

    gsmaestro

    17 AĞUSTOS 2006