HarmonyOS RN SDK CAPI新架构下,往沙箱里写文件写不进去的问题

RN SDK CAPI新架构下,提供的Demo SampleApp中往沙箱里写文件时,写不进去。代码未执行到。代码如下:

aboutToAppear(): void { 
  this.checkBundleUpdated() 
} 
 
checkBundleUpdated(): void { 
  if (this.rnAbility) {  // ----------------------------- 此处直接为false,导致未进入,执行不了后续的代码 
  const sandboxDir = this.rnAbility.context.filesDir 
  const bundlePath = sandboxDir + '/' + this.bundlePath 
  try { 
  const stat = fs.statSync(bundlePath) 
  if (stat.size == 0) { 
  this.downloadBundle() 
} else { 
  this.hasBundle = true 
} 
} catch (e) { 
  this.downloadBundle() 
} 
} 
}
HarmonyOS
2024-08-23 10:12:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

替换一下这两个文件

//第一个文件夹路径 SampleApp\entry\src\main\ets\entryability\EntryAbility.ets 
import { RNAbility, } from '@rnoh/react-native-openharmony'; 
 
import dataPreferences from '@ohos.data.preferences' 
import window from '@ohos.window'; 
 
export let preferences: dataPreferences.Preferences | null = null 
export default class EntryAbility extends RNAbility { 
  getPagePath() { 
    return 'pages/Index'; 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage) { 
    super.onWindowStageCreate(windowStage) 
    AppStorage.setOrCreate('RNAbility', this) 
    try { 
      let options: dataPreferences.Options = { 
        name: 'test' 
      }; 
      preferences = dataPreferences.getPreferencesSync(this.context, options) 
    } catch (err) { 
      console.error('Failed to get preferences') 
    } 
  } 
} 
 
//第二个文件夹路径 SampleApp\entry\src\main\ets\pages\SandboxBundle.ets 
import { 
  RNApp, 
  RNAbility, 
  FileJSBundleProvider, 
  ComponentBuilderContext, 
} from '@rnoh/react-native-openharmony' 
import fs from '@ohos.file.fs'; 
import { createRNPackages } from '../rn/RNPackagesFactory' 
import { LoadManager, buildCustomComponent } from '../rn/LoadBundle' 
 
const wrappedCustomRNComponentBuilder = wrapBuilder(buildCustomComponent); 
 
@Component 
export default struct SandBoxApp { 
  @Consume('pagePathsHome') pagePathsHome: NavPathStack; 
  @StorageLink('RNAbility') rnAbility: RNAbility | undefined = undefined; 
  // bundlePath 和 assetsPath 都是相对于 rnAbility.context.filesDir目录下的路径 
  bundlePath: string = 'sandbox.harmony.bundle' 
  assetsPath: string = 'assets' 
  @State hasBundle: Boolean = false 
  @State bundleStatus: string = '' 
 
  @Builder 
  PageMap(name: string) { 
    Text("Empty View") 
  } 
 
  @Builder 
  public buildCustomComponent(ctx: ComponentBuilderContext) { 
  } 
 
  aboutToAppear(): void { 
    this.checkBundleUpdated() 
  } 
 
  async downloadBundle() { 
    if (this.rnAbility) { 
      // 将bundlejs下载并保存到沙箱 
      let uint8Array = await this.rnAbility.context.resourceManager.getRawFileContent('rawfile/bundle/bp/sandbox.harmony.bundle') 
      let rawBuffer = uint8Array.buffer 
      let num = rawBuffer.byteLength 
      // 获取沙箱路径 
      const sandboxDir = this.rnAbility.context.filesDir 
      const bundlePath = sandboxDir + '/' + this.bundlePath 
      let stream = fs.createStreamSync(bundlePath, 'w') 
      stream.writeSync(rawBuffer) 
      stream.closeSync() 
      this.hasBundle = true 
    } 
  } 
  checkBundleUpdated(): void { 
    if (this.rnAbility) { 
      const sandboxDir = this.rnAbility.context.filesDir 
      console.warn(`tc123 path 1 ${this.rnAbility.context.filesDir}`) 
      console.warn(`tc123 path 2 ${getContext(this).filesDir}`) 
 
      const bundlePath = sandboxDir + '/' + this.bundlePath 
      try { 
        const stat = fs.statSync(bundlePath) 
        if (stat.size == 0) { 
          this.downloadBundle() 
        } else { 
          this.hasBundle = true 
        } 
      } catch (e) { 
        this.downloadBundle() 
      } 
    } 
  } 
 
  build() { 
    NavDestination() { 
      Row() { 
        Text('紫色区域由ArkTs渲染') 
          .fontSize(30) 
          .fontWeight(30) 
          .margin({ top: 10 }) 
      } 
      .height(100) 
      .width('90%') 
      .margin({ top: 10 }) 
      .backgroundColor('#a0a0ff') 
      .borderRadius(10) 
 
      if (this.rnAbility && this.hasBundle) { 
        RNApp({ 
          rnInstanceConfig: { createRNPackages }, 
          initialProps: { "foo": "bar" } as Record<string, string>, 
          appKey: "Sandbox", 
          wrappedCustomRNComponentBuilder: wrapBuilder(buildCustomComponent), 
          onSetUp: (rnInstance) => { 
            rnInstance.enableFeatureFlag("ENABLE_RN_INSTANCE_CLEAN_UP") 
          }, 
          jsBundleProvider: new FileJSBundleProvider(this.rnAbility.context.filesDir + '/' + this.bundlePath) 
        }) 
      } else { 
        Row() { 
          Text('未加载bundle') 
          Text(this.bundleStatus) 
        } 
        .height(100) 
      } 
    } 
    .hideTitleBar(true) 
  } 
}
分享
微博
QQ
微信
回复
2024-08-23 16:00:44
相关问题
phpstudymysql登不进去报2002怎么办?
2501浏览 • 1回复 待解决
HarmonyOS文件上传怎么
549浏览 • 1回复 待解决
HarmonyOS BLE数据问题
562浏览 • 1回复 待解决
求教一个sql语句传参怎么
3350浏览 • 1回复 待解决
fileio.writebuffer数据有问题
4628浏览 • 1回复 待解决
如果一个多级获取数据问题
3404浏览 • 1回复 待解决
在color.json文件注释报错如下
703浏览 • 1回复 待解决
鸿蒙promise要怎么
7446浏览 • 3回复 待解决
修改沙箱路径json文件指定内容
2400浏览 • 1回复 待解决
cookie读、和删除操作
306浏览 • 1回复 待解决
HarmonyOS SM3加密怎么呢?
41浏览 • 1回复 待解决
HarmonyOS RN三方库列表对应CAPI库列表
473浏览 • 1回复 待解决
ETs,对沙箱路径json文件内容遍历
3350浏览 • 1回复 待解决