android加载文件路径怎么写

1.android的项目里怎么规定文件路径的方法一:把目标文件放入resources文件中,以通过读取R的资源文件来获取,具体方式如下:1、在res下新建raw文件,将带读取文件添加到raw文件目录下 。
2、添加如下代码:// 如果要使用文件名获取文件数据:首先获取资源id然后再通过id获取输入流 InputStream im = getResources().openRawResource(R.raw.h_data11); BufferedReader read = new BufferedReader(new InputStreamReader(im)); String line = ""; StringBuilder sb = new StringBuilder(); try { while((line = read.readLine()) != null) { sb.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if(read != null) { try { read.close(); read = null; } catch (IOException e) { e.printStackTrace(); } }if(im != null) { try { im.close(); im = null; } catch (IOException e) { e.printStackTrace(); } } } Log.v("", "result = " + sb.toString());方法二:使用assets 只读文件进行读取 。1、将文件copy到assets下,可以新建文件夹如:“www”然后将文件放入www文件夹中,读取的path为:"www/filename"String result = "";ObjectInputStream ois = null; AssetManager am = context.getResources().getAssets(); try { ois = new ObjectInputStream(am.open("www/filename")); result = (String) ois.readObject(); } catch ( e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch ( e) { e.printStackTrace(); } finally { try { if (ois != null) { ois.close(); ois = null; } } catch (IOException e) { e.printStackTrace(); } }以对象的方式读取文件中的数据,如果没有新建文件夹,把前面的“www/”去掉就ok啦以上方式我都还有疑问的地方:1、raw下新建多级目录是否真的不能够使用 。
2.Android 在知道文件路径以及文件名称读取SD卡上的图片该怎么做String path = Environment.() ; //获得SDCard目录
Bitmap bmpDefaultPic;
ImageView iv = (ImageView) contentView.findViewById(R.id.x);
【android加载文件路径怎么写】if(bmpDefaultPic==null)
bmpDefaultPic = BitmapFactory.decodeFile(path+"xx.jpg",null);
iv.setImageBitmap(bmpDefaultPic);
R.id.x 代表你的ImageView
xx.jpg 你的图片
3.android studio引用assets文件夹里的东西路径怎么写1.默认Android Studio的assets文件夹路径:src/main/assets,在assets文件夹创建teachcourse.xml文件,引用该文件的写法:
InputStream is = null;AssetManager manager = getAssets();try { is = manager.open("teachcourse.xml");} catch (Exception e) { e.printStackTrace();}2.直接写上assets文件夹内文件的名称,使用AssetsManager管理器打开,获取输入流,最后解析里面的内容

android加载文件路径怎么写

文章插图