2

I am creating Android app with database SQLite.
I have tried to store Arabic text in my database. For 1 and 2 I tried to insert Arabic text and for 4 I inserted unicode from Arabic text.
enter image description here

String textDoa = "";
try {
    textDoa = new String(cursor.getString(cursor.getColumnIndex("DOA_ARAB")).getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

doaDetail.setDoaArab(Html.fromHtml(textDoa).toString());

That is my code for get data from database.
And I used the ArabicReshaper from this

TextView textDoa = new TextView(this);
try{
    textDoa.setTypeface(tf);
    textDoa.setText(ArabicUtilities.reshape(doa.getDoaArab()));
    //textDoa.setText(doa.getDoaArab());
    textDoa.setTextSize(17f);
} catch(Exception ex){
    textDoa.setText("font cannot load: "+ ex.toString() );
}

But the results is different what I expected.

For 1 the TextView appear : . الْحَمْد٠لÙلَّه٠الَّذÙÙŠ أَحْيَاناَ بَعْدَ مَا أَمَاتَنَا ÙˆÙŽØ¥ÙÙ„ÙŽÙŠÙ’Ù‡Ù Ø§Ù„Ù†Ù‘ÙØ´ÙوْرÙ

And for 4 appear as like as from database, didn't change to Arabic text.

Please help me to resolv this problem.

Community
  • 1
  • 1
ihsanhf
  • 430
  • 1
  • 9
  • 18

2 Answers2

0

Many people suffered this problem, I would suggest you to store as a text file with UTF-8 and store it in sql. then retrieve them as well. no big conversation needed.

and if you don't know how to read text file. for instance SDCard

 //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');
     }
 }
 catch (IOException e) {
     //You'll need to add proper error handling here
 }

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

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

Original Post(How can I read a text file from the SD card in Android?)

Community
  • 1
  • 1
Kirk
  • 4,957
  • 2
  • 32
  • 59
0

Solution Arabic text in SQLite Solution to this problem is keeping the data through the Arab command (must be Insert Data by code (Insert into Table_name ( )Values( ))don't insert data on SQlite direct this for Arabic but English no problem )

public class SQLiteAdapter
public static final String MYDATABASE_NAME = "TestSQLilte.s3db";
@SuppressLint("SdCardPath")
// Do not forget This code DB_DIR Because the program does not work on Mobile     without this code
    private static String DB_DIR = "/data/data/PackageName/databases/";
   @SuppressWarnings("unused")
 // Do not forget This code DB_DIR Because the program does not work on Mobile  without this code

   private static String DB_PATH = DB_DIR + MYDATABASE_NAME;
   public static final String MYDATABASE_TABLE = "Table_Name";
   public static final int MYDATABASE_VERSION = 2;
   public static final String KEY_ID = "_id";
   public static final String KEY_Name = "Name";
   public static final String KEY_Adress = "Address";
   public static final String KEY_Area = "Phone";

   private static final String SCRIPT_CREATE_DATABASE =
          "create table  IF NOT EXISTS " + MYDATABASE_TABLE + " ("
          + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
          + KEY_Name + " Text not null"
                + ", "
          + KEY_Adress + " Text not null"
                + ", "
          + KEY_Phone + " Text not null);";
  private Context context;
  public SQLiteAdapter(Context c){
  context = c;
 } 

// Do not forget This code DB_DIR Because the program does not work on Mobile without this code public SQLiteAdapter openToRead() throws android.database.SQLException {

       sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,    MYDATABASE_VERSION);

        DB_PATH = context.getDatabasePath(MYDATABASE_NAME).getAbsolutePath();

        sqLiteDatabase = sqLiteHelper.getReadableDatabase();

       return this; 
     }

// Do not forget This code DB_DIR Because the program does not work on Mobile without this code public SQLiteAdapter openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);

            DB_PATH = context.getDatabasePath(MYDATABASE_NAME).getAbsolutePath();
           sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
          return this; 
         }
 // Do not forget This code mySQLiteAdapter.insert Because the program does not Display arabic without this code
  public long insert(String Name,String Adress,String Phone){
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_About_Content, Name);
        contentValues.put(KEY_Adress,Adress);
        contentValues.put(KEY_Area ,Area);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
         }  
  public class MainActivity
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Test);
  mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();
  // Do not forget This code mySQLiteAdapter.insert Because the program does not  Display arabic without this code
  mySQLiteAdapter.insert    ("1الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("2الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("3الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("4الاسم","العنوان","التلفون");
 }

I've been suffering from this problem, thank God, it has been resolved in this wa