代码之家  ›  专栏  ›  技术社区  ›  KIM JONG YUNG

Xamarin Android使用Mupdf在布局中查看pdf

  •  0
  • KIM JONG YUNG  · 技术社区  · 7 年前

    我想在Xamarin Android项目中使用MuPDF阅读器。 我试图在我的相对布局中查看PDF

    这是我的相关布局代码

      <RelativeLayout
       android:id="@+id/mupdf_wrapper"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
      </RelativeLayout>
    

    这是主要的活性

    SetContentView(Resource.Layout.Main);
    
            RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);
    
    
    
            MuPDFCore core = new MuPDFCore(this, "test.pdf");
            MuPDFReaderView reader = new MuPDFReaderView(this);
            reader.Adapter = new MuPDFPageAdapter(this, new FilePicker.IFilePickerSupport() , core);
            mupdfWrapper.AddView(reader);
    
            mupdfWrapper.AddView(reader);
    

    但我在这里犯了个错误

    “无法创建抽象类或接口”文件选择器的istance。iflepickersupport”

    谁能帮我解决这个问题。

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   PixelDev    7 年前

    如果您没有使用FilePicker。IFilePickerSupport(),然后将其设置为空 喜欢

    reader.Adapter = new MuPDFPageAdapter(this, null , core);
    

    第二件事是你们的代码对我很有帮助,你们面临的问题,但你们的问题是我在我的项目中的解决方案,所以谢谢你们。 尝试一下,它会起作用,我在我的代码中使用它,它对我很有用。 最后一件事是对不起我的英语。

         protected override void OnCreate(Bundle savedInstanceState)
         {
                    base.OnCreate(savedInstanceState);
                    File fileToDisplay = (File)fileFromAsset(this, "test.pdf");
                    fileToDisplay.SetWritable(true);
                    RelativeLayout mupdfWrapper = FindViewById<RelativeLayout>(Resource.Id.mupdf_wrapper);
                    MuPDFCore core = new MuPDFCore(this, fileToDisplay.AbsolutePath);
    
                    MuPDFReaderView reader = new MuPDFReaderView(this);
                    MuPDFPageAdapter adapter = new MuPDFPageAdapter(this, null, core);
                    reader.SetAdapter(adapter);
                    mupdfWrapper.AddView(reader);
        }
    
        private object fileFromAsset(Context context, string assetName)
        {
            File outFile = new File(context.CacheDir, assetName);
            copy(context.Assets.Open(assetName), outFile);
            return outFile;
        }
    
        private void copy(Stream inputStream, File output)
        {
            OutputStream outputStream = null;
            var bufferedInputStream = new BufferedInputStream(inputStream);
            try
            {
                outputStream = new FileOutputStream(output);
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = bufferedInputStream.Read(bytes)) != -1)
                {
                    outputStream.Write(bytes, 0, read);
                }
            }
            finally
            {
                try
                {
                    if (inputStream != null)
                    {
                        inputStream.Close();                  
                        inputStream.Dispose();
                        inputStream = null;
                    }
                }
                finally
                {
                    if (outputStream != null)
                    {
                        outputStream.Close();                        
                        outputStream.Dispose();
                        outputStream = null;
                    }
                }
            }
        }