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() 
} 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
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) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
分享
微博
QQ
微信
回复
2024-08-23 16:00:44


相关问题
phpstudymysql登不进去报2002怎么办?
2889浏览 • 1回复 待解决
HarmonyOS文件上传怎么
1383浏览 • 1回复 待解决
HarmonyOS BLE数据问题
1329浏览 • 1回复 待解决
HarmonyOS 怎么在代码循环标签
583浏览 • 1回复 待解决
HarmonyOS RN capi生成Codegen失败
777浏览 • 1回复 待解决
求教一个sql语句传参怎么
4266浏览 • 1回复 待解决
HarmonyOS 预览沙箱路径文件失败
657浏览 • 1回复 待解决
如果一个多级获取数据问题
4106浏览 • 1回复 待解决
fileio.writebuffer数据有问题
5172浏览 • 1回复 待解决
在color.json文件注释报错如下
1426浏览 • 1回复 待解决
HarmonyOS反射该怎么
554浏览 • 1回复 待解决