Jak uzyskać URI z pliku zasobu?

Próbowałem uzyskać ścieżkę URI dla pliku zasobu.

uri = Uri.fromFile(new File("//assets/mydemo.txt"));

Kiedy sprawdzam, czy plik istnieje, widzę, że plik nie istnieje

File f = new File(filepath);
if (f.exists() == true) {
    Log.e(TAG, "Valid :" + filepath);
} else {
    Log.e(TAG, "InValid :" + filepath);
}

Czy ktoś może mi powiedzieć, jak mogę podać ścieżkę bezwzględną dla pliku istniejącego w folderze zasobu

Author: klefevre, 2011-01-27

10 answers

Nie ma "bezwzględnej ścieżki dla pliku istniejącego w folderze zasobu". Zawartość folderu assets/ twojego projektu jest zapakowana w plik APK. Użyj AssetManager obiekt, aby uzyskać InputStream na zasobie.

EDIT

Aby naprawić jeden z moich komentarzy poniżej, składnia URL dla zasobów to file:///android_asset/... (Uwaga: trzy ukośniki).

 130
Author: CommonsWare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-04 12:17:35

Prawidłowy adres url to:

file:///android_asset/RELATIVEPATH

Gdzie RELATIVEPATH jest ścieżką do zasobu względem folderu assets.

Uwaga 3 / ' s W schemacie. Widok sieci Web nie załaduje żadnego z moich zasobów bez 3. Próbowałem 2 Jak (wcześniej) skomentował CommonsWare i nie działa. Następnie spojrzałem na źródło CommonsWare na GitHubie i zauważyłem dodatkowy ukośnik.

Ten test został jednak wykonany tylko na emulatorze Androida 1.6, ale wątpię, aby był inny na prawdziwym urządzenie lub wyższa wersja.

EDIT: CommonsWare zaktualizował swoją odpowiedź, aby odzwierciedlić tę drobną zmianę. Więc edytowałem to tak, że nadal ma sens z jego aktualną odpowiedzią.

 64
Author: Russ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-17 19:24:55

Tutaj wpisz opis obrazka

Upewnij się, że folder zasobów znajduje się we właściwej pozycji.

 7
Author: Chuanhang.gu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-31 12:22:39

Działa dla WebView, ale wydaje się, że nie działa na URL.openStream(). Musisz więc rozróżnić protokoły file: / / i obsługiwać je poprzez AssetManager zgodnie z sugestią.

 6
Author: Gaylord Zach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-05 14:32:11

Proszę wypróbować ten kod działa poprawnie

 Uri imageUri = Uri.fromFile(new File("//android_asset/luc.jpeg"));

    /* 2) Create a new Intent */
    Intent imageEditorIntent = new AdobeImageIntent.Builder(this)
            .setData(imageUri)
            .build();
 4
Author: Raaz Thakur,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-04 07:12:26

Wypróbuj to: to działa

InputStream in_s = getClass().getClassLoader().getResourceAsStream("TopBrands.xml");

Jeśli masz wyjątek wartości null spróbuj tego:

InputStream in_s1 =   TopBrandData.class.getResourceAsStream("/assets/TopBrands.xml");

TopBranData jest klasą

 3
Author: Jaspreet Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-05 16:52:39
InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
 2
Author: jayesh kavathiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-03 07:07:16

Tak, nie możesz uzyskać dostępu do folderu dysku z telefonu lub emulatora z Androidem, ponieważ twój komputer i android to dwa różne systemy operacyjne.Wybrałbym folder res Androida, ponieważ ma dobre metody zarządzania zasobami. Dopóki nie masz bardzo dobrego powodu, aby umieścić plik w folderze zasoby. Zamiast tego możesz to zrobić

try {
      Resources res = getResources();
      InputStream in_s = res.openRawResource(R.raw.yourfile);

      byte[] b = new byte[in_s.available()];
      in_s.read(b);
      String str = new String(b);
    } catch (Exception e) {
      Log.e(LOG_TAG, "File Reading Error", e);
 }
 0
Author: willnotquit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-18 08:14:22

Spróbuj tego:

Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.cat); 

I had did it and it work

 -1
Author: Nyle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-21 01:42:18

Zadziałało dla mnie spróbuj tego kodu

   uri = Uri.fromFile(new File("//assets/testdemo.txt"));
    File f = new File(testfilepath);
    if (f.exists() == true) {
    Toast.makeText(getApplicationContext(),"valid :" + testfilepath, 2000).show();
    } else {
   Toast.makeText(getApplicationContext(),"invalid :" + testfilepath, 2000).show();
 }
 -4
Author: Irfan Ali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-20 14:23:06