android开发如何获取res/raw和assets文件夹的路径

 android开发如何获取res/raw和assets文件夹的路径,主要分为两种情况:

    1.如果你只是拷贝动作,那么你只需要得到res/raw和assets文件输入流就可以,方法如下:

    获取res/raw文件输入流:  InputStream is=getResources().openRawResource(R.raw.XXX);
    获取assets文件输入流:  InputStream is=getResources().getAssets().open("文件名");

public static Bitmap getImageFromAssetsFile(String fileName, Context content)
    {
        Bitmap image = null;
        AssetManager am = content.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;

    }

 

    2.如果你想对res/raw和assets文件写的动作,那就要得到相应uri,方法如下:

    获取res/raw的uri:Uri uri=Uri.paese("android:resource://包名/"+R.raw.xxx);

String path = new File( "android.resource://"+getPackageName()+"/"+R.drawable.blue_plate).getAbsolutePath();
path = getFileStreamPath("plate_locate.jpg").getAbsolutePath();

 

MediaController mediaController=new MediaController(this);
String uri="android.resource://"+getPackageName()+"/"+R.raw.test2;
videoView.setVideoURI(Uri.parse(uri));
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);

 

/**
     * 从raw包下读取数据, demo: TaskDetail gsonObj = new Gson().fromJson(IoriFileHelper.readFileFromRaw(this,R.raw.json), TaskDetail.class);
     * @param context
     * @param rawName   R.raw.jx 
     * @return
     */
    public static String readFileFromRaw(Activity context, int rawName)
    {
        try {
            InputStreamReader inputReader = new InputStreamReader(context.getResources().openRawResource(rawName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String result = "";
            while ((line = bufReader.readLine()) != null)
                result += line;
            return result;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * @param activity
     * @param fileName  main.json
     * @return
     */
    private static String getFileStremFromAsserts(Activity activity,String fileName)
    {

        try {
            //从assets获取json文件
            InputStreamReader isr = new InputStreamReader(activity.getClass().getClassLoader().getResourceAsStream("assets/" + fileName));
            //字节流转字符流
            BufferedReader bfr = new BufferedReader(isr);
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bfr.readLine()) != null) {
                stringBuilder.append(line);
            }//将JSON数据转化为字符串
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }