android uri pdf,Render PDF from URI in Android?

I'm using Android Render in my app, but I've got problems with getting a PDF file from URI.

Here's how I get the URI of the file:

public void performFileSearch() {

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

intent.addCategory(Intent.CATEGORY_OPENABLE);

intent.setType("application/pdf");

startActivityForResult(intent, READ_REQUEST_CODE);

}

@Override

public void onActivityResult(int requestCode, int resultCode,

Intent resultData) {

if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

uri = null;

if (resultData != null) {

uri = resultData.getData();

pageNumber = 0;

render(uri);

}

}

}

Then in render(Uri uri):

private void render(Uri uri) {

try {

img = (ImageView) findViewById(R.id.image);

int width = img.getWidth();

int height = img.getHeight();

previous.setText(uri.toString());

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

File file = new File(uri.getPath());

PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));

if(currentpage<0){

currentpage =0;

}else if(currentpage>renderer.getPageCount()){

currentpage = renderer.getPageCount() -1;

Matrix matrix = img.getImageMatrix();

Rect rect= new Rect(0,0, width , height);

renderer.openPage(currentpage).render(bitmap,rect,matrix , PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY );

img.setImageMatrix(matrix);

img.setImageBitmap(bitmap);

img.invalidate();

}

} catch (Exception e) {

e.printStackTrace();

Toast.makeText(getApplicationContext(), "Nope", Toast.LENGTH_LONG);

}

}

The activity only shows white screen. I guess that the problem is with creating file from URI. Do you know how to make file from URI that I got? Thanks in advance.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值