0

`# Dears, this class works well to receive PDF files from another application on below Android 6, but it gives an error for Android uper 6


          `                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                public int ExtractImage(Intent intent) {
                    try {
                        String filepath = null;
            
                        if (intent != null) {
            
                            String action = intent.getAction();
            
                            String type = intent.getType();
            
                            if (Intent.ACTION_VIEW.equals(action) && type.endsWith("pdf")) {
            
            
                                Uri file_uri = intent.getData();
            
                                if (file_uri != null) {
            
                                    filepath = file_uri.getPath();
                                }
            
                            } else if (Intent.ACTION_SEND.equals(action) && type.endsWith("pdf")) {
            
                                Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
            
                                if (uri != null) {
                                    filepath = uri.getPath();
                                }
            
                            }
                        }
            
                        File file = new File(filepath);
            
                        PdfRenderer renderer = null;
                        Bitmap bm;
            
                        try {
                          renderer = new PdfRenderer(ParcelFileDescriptor.open(file,  

                          ParcelFileDescriptor.MODE_READ_ONLY));
                        } catch (Exception e) {
                            
                        }
            
                        assert renderer != null;
                        final int pageCount = renderer.getPageCount();
            
                        totalPage = pageCount;
            
                        for (int i = 0; i < pageCount; i++) {
            
                            PdfRenderer.Page page = renderer.openPage(i);
                         
                            // Create a bitmap and canvas to draw the page into
                            int width = 570;
                            int zarib = 570 / (page.getWidth() + 1);
                            int height = (page.getHeight() * 2) + 1;
                            heightArray.add(i, height);
            
                            // Create a bitmap and canvas to draw the page into
                            bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            
                            // Create canvas to draw into the bitmap
                            Canvas c = new Canvas(bm);
            
                            // Fill the bitmap with a white background
                            Paint whiteBgnd = new Paint();
                            whiteBgnd.setColor(Color.WHITE);
                            whiteBgnd.setStyle(Paint.Style.FILL);
                            c.drawRect(0, 0, width, height, whiteBgnd);
            
                            // paint the page into the canvas
                            page.render(bm, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
            
                            // Save the bitmap
                            OutputStream outStream = null;
                            try {
                                outStream = new                                                  

                           FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()
                           + "/printDo2ta" + i + ".png");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            bm.compress(Bitmap.CompressFormat.PNG, 80, outStream);
            
                            try {
                                outStream.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
            
                            page.close();
                        }
            
                    } catch (Exception e) {
                        runOnUiThread(() -> Toast.makeText(getBaseContext(),
                                "خطا در پردازش فایل: " + e.getMessage(),
                                Toast.LENGTH_SHORT).show());
                    }
            
                    return totalPage;
                }

I inserted this code in AndroidManifest.xml

     `<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="ir.myproject.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
     </provider>`

`

And I made the class provider_paths.xml

  `<?xml version="1.0" encoding="utf-8"?>
   <paths>
     <external-path name="external_files" path="."/>
   </paths>`

I don't know what else I need to change to make it work on Android 8

please help me`

tirozh
  • 1
  • 1

0 Answers0