首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从JS Stream加载(与现有应用集成)?非JS捆绑包文件路径

如何从JS Stream加载(与现有应用集成)?非JS捆绑包文件路径
EN

Stack Overflow用户
提问于 2015-12-16 21:16:17
回答 1查看 417关注 0票数 2

我发现react-native从android的默认文件目录加载react Js,我想要一个api从流加载react Js,而不是文件路径。因此,我可以从网络服务器加载Js,而不是将Js包文件放在本地目录中。

代码语言:javascript
复制
 String sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    String bundleFile = sdDir + "/Download/ReactNativeDevBundle.js";
    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setJSBundleFile(bundleFile)
            .addPackage(new MainReactPackage())
            .setUseDeveloperSupport(false)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    mReactRootView.startReactApplication(mReactInstanceManager, "MyAwesomeApp", null);
    setContentView(mReactRootView);

库版本: react-native:0.15.0

我可以更改默认的加载目录,但不能更改文件名。我发现没有办法从流中加载Js。

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 19:54:33

我最近遇到了同样的问题,并解决了这个问题。我回答这个问题来得太晚了,这可能会对某人有所帮助。

当我们试图从资源文件夹加载index.android.bunddle时,它会搜索到路径drawable-mdpi文件夹,不管您在react本地文件夹中给出的图像名称是什么,它都会在可绘制文件夹中创建图像。

并挑选该图像并显示给服务器的情况下,index.android.bundle,我们将文件存储在mob设备中,所以它找不到存储中的图像,我们的存储不包含可绘制的文件夹。因此,解决方案是我们需要创建密度可绘制的文件夹,如drawable-mdpi,并将图像写入到与index.android.bundle平行的移动存储中。

下面是我的代码片段

代码语言:javascript
复制
    int lengthOfFile = connection.getContentLength();
    //            InputStream input= new BufferedInputStream(url.openStream(),lengthOfFile);
                input = connection.getInputStream();
    //            FileOutputStream 

outPut=context.openFileOutput("index.android.bundle",Context.MODE_PRIVATE);
                FileOutputStream outPut = null;
                File directory,file;

     directory = new File(context.getFilesDir(), "drawable-xxhdpi");
                        if(!directory.exists()){
                            directory.mkdir();
                        }
  file = new File(context.getFilesDir() + File.separator + "drawable-xxhdpi", "src_components_images_iconthumbsdowndefault.png");
                        outPut = new FileOutputStream(file);

      byte data[]=new byte[4096];
                long total=0;
                int x = 0;
                while ((count = input.read(data)) != -1){
                    total+=count;
                    x++;
    //                publishProgress(""+(int)((total*100)/lengthOfFile));
                    outPut.write(data,0,count);
                }
                outPut.flush();
                outPut.close();
                input.close();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34313086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档