React-Native, Pdf showing on android simulator but getting error on actual android device

℡╲_俬逩灬. 提交于 2020-06-01 06:22:11

问题


I have installed react-native-pdf and rn-fetch-blob packages to show pdf file.It work's fine on simulator but for some reason i am getting "Error: open failed: ENOENT (No such file or directory)".

Here is my code below:

import React, { Component } from 'react';
import {
  StyleSheet,
  Dimensions,
  View
} from 'react-native';

import Pdf from 'react-native-pdf';


// Screen that shows the contents of a PDF
export default class OpenPdf extends Component {

  static navigationOptions = {
    title: 'Product Variants'
  };

  render() {
    const source = require('../assets/Product_Variants_Electric_Connections.pdf');

    return (
        <View style={{width: '100%', height: '100%'}}>
          <Pdf style={styles.pdf}
          source={source}

          onLoadComplete={(numberOfPages, filePath) => {
              alert(`number of pages: ${numberOfPages}`);
          }}
          onPageChanged={(page, numberOfPages) => {
              alert(`current page: ${page}`);
          }}
          onError={(error) => {
              alert(`error`+error);
          }}
      />
      </View>
  );
  }
}

const styles = StyleSheet.create({
  container: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center'
  },
  pdf: {
      flex: 1,
      width: Dimensions.get('window').width
  }
});

I have already went through many links, none of the issue is similar to mine. Kindly please suggest what am I missing.


回答1:


As suggested by @Hardik Virani above in comment, I have removed local url and did like below:

render() {
    const source = {uri:'bundle-assets://Product_Variants_Electric_Connections.pdf', cache: true};

    return (
        <View style={{width: '100%', height: '100%'}}>
          <Pdf style={styles.pdf}
          source={source}

      />
      </View>
  );

And Remember Just put your actual pdf in the android/app/src/main/assets/pdf folder of your project.



来源:https://stackoverflow.com/questions/55805955/react-native-pdf-showing-on-android-simulator-but-getting-error-on-actual-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!