البرمجة

قراءة ملف نصي من drawable في Android

To read a .txt file from the drawable folder or any other folder in an Android app using FileInputStream, you need to first copy the file to the internal storage of the app and then read it. Here’s how you can do it:

  1. Copy the File to Internal Storage:

    • First, create a method to copy the file from the drawable folder to the internal storage of your app:
    java
    private void copyFileToInternalStorage() { try { InputStream inputStream = getResources().openRawResource(R.drawable.your_txt_file); FileOutputStream fileOutputStream = openFileOutput("your_txt_file.txt", Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
    • Replace R.drawable.your_txt_file with the resource ID of your .txt file in the drawable folder.
  2. Read the File and Set it in TextView:

    • Next, read the file from the internal storage and set it in a TextView:
    java
    private void readFileAndSetInTextView() { try { FileInputStream fileInputStream = openFileInput("your_txt_file.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } TextView textView = findViewById(R.id.textView); textView.setText(stringBuilder.toString()); bufferedReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
    • Make sure to call copyFileToInternalStorage() before calling readFileAndSetInTextView() to ensure that the file is copied to the internal storage.
  3. Permissions:

    • Don’t forget to add the necessary permissions in your AndroidManifest.xml file:
    xml
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  4. Usage:

    • Call readFileAndSetInTextView() in your activity’s onCreate() method to read the file and set it in the TextView.

This approach copies the file from the drawable folder to the internal storage for reading, as direct access to files in the drawable folder is not possible.

المزيد من المعلومات

قراءة ملف نصي (.txt) من مجلد drawable أو أي مجلد آخر في تطبيق Android يتطلب القليل من الخطوات الإضافية بسبب الفروق في طرق الوصول إلى الملفات في المجلدات المختلفة.

  1. تحميل الملف إلى الذاكرة الداخلية:

    • يمكنك استخدام الرمز التالي لنسخ الملف من مجلد drawable إلى الذاكرة الداخلية للتطبيق:
    java
    private void copyFileToInternalStorage() { try { InputStream inputStream = getResources().openRawResource(R.drawable.your_txt_file); FileOutputStream fileOutputStream = openFileOutput("your_txt_file.txt", Context.MODE_PRIVATE); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
    • قم بتعويض R.drawable.your_txt_file بمعرف المورد الخاص بملف .txt في مجلد drawable.
  2. قراءة الملف وتعيينه في TextView:

    • بعد ذلك، قم بقراءة الملف من الذاكرة الداخلية وتعيينه في TextView:
    java
    private void readFileAndSetInTextView() { try { FileInputStream fileInputStream = openFileInput("your_txt_file.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } TextView textView = findViewById(R.id.textView); textView.setText(stringBuilder.toString()); bufferedReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
    • تأكد من استدعاء copyFileToInternalStorage() قبل استدعاء readFileAndSetInTextView() لضمان نسخ الملف إلى الذاكرة الداخلية.
  3. الصلاحيات:

    • لا تنسَ إضافة الصلاحيات اللازمة في ملف AndroidManifest.xml:
    xml
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  4. استخدام:

    • قم بإستدعاء readFileAndSetInTextView() في طريقة onCreate() في النشاط الخاص بك لقراءة الملف وتعيينه في TextView.

هذه الطريقة تنسخ الملف من مجلد drawable إلى الذاكرة الداخلية للقراءة، لأن الوصول المباشر إلى الملفات في مجلد drawable غير ممكن.

مقالات ذات صلة

زر الذهاب إلى الأعلى

هذا المحتوى محمي من النسخ لمشاركته يرجى استعمال أزرار المشاركة السريعة أو تسخ الرابط !!