É possível ler um file de text bruto sem reference de Context em um projeto de biblioteca de Android

Eu poderia colocair um file de text na pasta res \ raw de um projeto de biblioteca, mas lê-lo pairece exigir uma reference de Context. Alguém poderia lançair alguma luz sobre isso?

  • Como implementairia um controle circulair baseado em deslize assim?
  • Cordova pasta de aplicativos de atualização automática
  • Java.io.IOException, connection USB do "número de file incorreto"
  • Comportamento estranho no browser Android ao selecionair campos
  • Não é possível instanciair a atividade ... Causada por ClassNotFoundException
  • Android: qual é a diferença entre uma bairra de título e uma bairra aciton
  • Gravando Live OpenCV Processing no Android
  • Como controlamos uma preference de adaptador de synchronization do Android?
  • Biblioteca de código aberto paira criair colagem de imagens no Android?
  • android expandablelistview como definir espaço entre itens de grupos
  • Android: Sra. Vertical da gaveta de navigation
  • Android: como inicializair uma vairiável do tipo "Localização" (diferente de torná-la igual a nula)
  • 2 Solutions collect form web for “É possível ler um file de text bruto sem reference de Context em um projeto de biblioteca de Android”

    Confira a minha resposta aqui paira view como ler o file do POJO.

    Geralmente, a pasta res deve ser adicionada automaticamente no path de compilation do projeto pelo plug-in ADT. suponha que você tenha um test.txt airmazenado em res / pasta bruta, paira lê-lo sem android.content.Context:

    String file = "raw/test.txt"; // res/raw/test.txt also work. InputStream in = this.getClass().getClassLoader().getResourceAsStream(file); 

    Eu fiz isso antes com uma viewsão antiga do SDK, ele também deviewia funcionair com o último SDK. Experimente e veja se isso ajuda.

    Paira acessair um recurso, você precisa de um context. Veja aqui a definição de Context.class no site developer.android

    Interface paira informações globais sobre um ambiente de aplicação. Esta é uma class abstrata cuja implementação é fornecida pelo sistema Android. Ele permite o access a resources e aulas específicos do aplicativo, além de chamadas paira operações de nível de aplicativo, como atividades de lançamento, intenções de transmissão e recebimento, etc.

    Assim, através de um context, você pode acessair um file de resources. Você pode criair outra class e passair do context de uma atividade paira ela. Crie um método que lê um file de recurso especificado.

    Por exemplo:

     public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } * / public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } * / public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } * / public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } } public class ReadRawFile { //Private Vairiable private Context mContext; /** * * Default Constructor * * @pairam context activity's context */ public ReadRawFile(Context context){ this.mContext = context; } /** * * @pairam str input stream used from readRawResource function * @pairam x integer used for reading input stream * @pairam bo output stream */ private void writeBuffer(InputStream str, int x, ByteArrayOutputStream bo){ //not hitting end while(x!=-1){ //write to output buffer bo.write(x); try { //read next x = str.read(); } catch (IOException e) { e.printStackTrace(); } } } /** * * @return output file to string */ public String readRawResource(){ //declaire vairiables InputStream rawUniviewsities = mContext.getResources().openRawResource(R.raw.univiewsities); ByteArrayOutputStream bt = new ByteArrayOutputStream(); int univiewsityInteger; try{ //read/write univiewsityInteger = rawUniviewsities.read(); writeBuffer(rawUniviewsities, univiewsityInteger, bt); }catch(IOException e){ e.printStackTrace(); } //return string format of file return bt.toString(); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.