2012-03-17 13 views
10

Chcę po prostu utworzyć obiekt pliku, podobnie jak toJak odwoływać się do pliku w surowej folderu w Androidzie

File myImageFile = new File ("image1") ;

ale to daje mi wyjątek FileNotFoundException
Jak mogę odwołać się złożyć w mojej surowej Folder

EDIT: Właściwie chciałem zrobić coś takiego

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("uploaded", new FileBody(new File("myimage")));

+0

i gdzie jest folder, twoje zdjęcia? –

+0

@djaqeel Przepraszam surowy folder nie obrazy folderu (który jest wewnątrz folderu res) .. również próbowałem uzyskać dostęp do obrazów w folderze rysowania, ale uzyskanie tego samego błędu –

+0

czy naprawdę potrzebujesz jako plik, czy strumień wejściowy działa dla Ciebie? –

Odpowiedz

3

tutaj są 2 funkcje. jeden do odczytu z RAW i jeden z aktywów

/** 
* Method to read in a text file placed in the res/raw directory of the 
* application. The method reads in all lines of the file sequentially. 
*/ 

public static void readRaw(Context ctx,int res_id) { 

    InputStream is = ctx.getResources().openRawResource(res_id); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer 
                 // size 

    // More efficient (less readable) implementation of above is the 
    // composite expression 
    /* 
    * BufferedReader br = new BufferedReader(new InputStreamReader(
    * this.getResources().openRawResource(R.raw.textfile)), 8192); 
    */ 

    try { 
     String test; 
     while (true) { 
      test = br.readLine(); 
      // readLine() returns null if no more lines in the file 
      if (test == null) 
       break; 
     } 
     isr.close(); 
     is.close(); 
     br.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

iz folderze Assets

/** 
* Read a file from assets 
* 
* @return the string from assets 
*/ 

public static String getQuestions(Context ctx,String file_name) { 

    AssetManager assetManager = ctx.getAssets(); 
    ByteArrayOutputStream outputStream = null; 
    InputStream inputStream = null; 
    try { 
     inputStream = assetManager.open(file_name); 
     outputStream = new ByteArrayOutputStream(); 
     byte buf[] = new byte[1024]; 
     int len; 
     try { 
      while ((len = inputStream.read(buf)) != -1) { 
       outputStream.write(buf, 0, len); 
      } 
      outputStream.close(); 
      inputStream.close(); 
     } catch (IOException e) { 
     } 
    } catch (IOException e) { 
    } 
    return outputStream.toString(); 

} 
+0

Jeśli chodzi o metodę "readRaw", po dojściu do końca pliku zmienna testowa staje się pusta, a zatem staje się bezużyteczna. – John61590

1

Można otworzyć go jako InputStream, nie wiem, jeśli to możliwe, w postaci pliku:

int rid = resources.getIdentifier(packageName + ":raw/" + fileName, null, null); 
//get the file as a stream 
InputStrea is = resources.openRawResource(rid); 
+0

, jeśli zna nazwę pliku, lepiej użyć opcji 'R.raw.id'. –

+0

@BorisStrandjev - Zgadzam się, ale czasami ludzie wolą robić to w ten sposób. – MByD

+0

Czytałem w wielu miejscach, że nie jest to wskazane: trudniejsze do kodowania iz dużo gorszą wydajnością. –

7

Generalnie dostęp do plików poprzez getResources() openRawResource (R.id.. _tutaj). Jeśli koniecznie potrzebujesz referencyjny nadany do niego, jedną z opcji jest, aby skopiować go do pamięci wewnętrznej:

File file = new File(this.getFilesDir() + File.separator + "DefaultProperties.xml"); 
try { 
     InputStream inputStream = resources.openRawResource(R.id._your_id); 
     FileOutputStream fileOutputStream = new FileOutputStream(file); 

     byte buf[]=new byte[1024]; 
     int len; 
     while((len=inputStream.read(buf))>0) { 
      fileOutputStream.write(buf,0,len); 
     } 

     fileOutputStream.close(); 
     inputStream.close(); 
    } catch (IOException e1) {} 

Teraz masz File że można uzyskać dostęp wszędzie tam, gdzie jest to potrzebne.

+0

To mi bardzo pomogło! Dziękuję Ci! Potrzebowałem pliku, aby utworzyć Uri do pliku Intent.EXTRA_STREAM przechowywanego w surowym folderze. – TomaszRykala

1

Można użyć InputStreamBody zamiast FileBody więc można go używać tak:

InputStream inputStream = resources.openRawResource(R.raw.yourresource); 

MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
multipartEntity.addPart("uploaded", new InputStreamBody(inputStream)); 
Powiązane problemy