我发现react-native从android的默认文件目录加载react Js,我想要一个api从流加载react Js,而不是文件路径。因此,我可以从网络服务器加载Js,而不是将Js包文件放在本地目录中。
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。
发布于 2018-05-28 19:54:33
我最近遇到了同样的问题,并解决了这个问题。我回答这个问题来得太晚了,这可能会对某人有所帮助。
当我们试图从资源文件夹加载index.android.bunddle时,它会搜索到路径drawable-mdpi文件夹,不管您在react本地文件夹中给出的图像名称是什么,它都会在可绘制文件夹中创建图像。
并挑选该图像并显示给服务器的情况下,index.android.bundle,我们将文件存储在mob设备中,所以它找不到存储中的图像,我们的存储不包含可绘制的文件夹。因此,解决方案是我们需要创建密度可绘制的文件夹,如drawable-mdpi,并将图像写入到与index.android.bundle平行的移动存储中。
下面是我的代码片段
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();
https://stackoverflow.com/questions/34313086
复制相似问题