J'ai un fichier texte ajouté en tant que ressource brute. Le fichier texte contient du texte comme :
b) IF APPLICABLE LAW REQUIRES ANY WARRANTIES WITH RESPECT TO THE
SOFTWARE, ALL SUCH WARRANTIES ARE
LIMITED IN DURATION TO NINETY (90)
DAYS FROM THE DATE OF DELIVERY.
(c) NO ORAL OR WRITTEN INFORMATION OR
ADVICE GIVEN BY VIRTUAL ORIENTEERING,
ITS DEALERS, DISTRIBUTORS, AGENTS OR
EMPLOYEES SHALL CREATE A WARRANTY OR
IN ANY WAY INCREASE THE SCOPE OF ANY
WARRANTY PROVIDED HEREIN.
(d) (USA only) SOME STATES DO NOT
ALLOW THE EXCLUSION OF IMPLIED
WARRANTIES, SO THE ABOVE EXCLUSION MAY
NOT APPLY TO YOU. THIS WARRANTY GIVES
YOU SPECIFIC LEGAL RIGHTS AND YOU MAY
ALSO HAVE OTHER LEGAL RIGHTS THAT
VARY FROM STATE TO STATE.
Sur mon écran, j'ai une disposition comme celle-ci :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:layout_below="@+id/logoLayout"
android:background="@drawable/list_background">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtRawResource"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"/>
</ScrollView>
</LinearLayout>
Le code pour lire la ressource brute est :
TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource);
txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample);
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
return null;
}
return byteArrayOutputStream.toString();
}
Le texte s'affiche, mais après chaque ligne, j'obtiens les caractères étranges suivants []
. Comment puis-je supprimer ces caractères ? Je pense que c'est un saut de ligne.
3 votes
Conseil : vous pouvez annoter votre paramètre rawRes avec @RawRes afin qu'Android Studio s'attende à des ressources brutes.
0 votes
La solution de travail doit être publiée en tant que réponse, où elle peut être votée.