0

Hi in the below when click on create pdf I am calling method create pdf .Click on the button pdf scrollview is created.

But nothing is displaying after click on create pdf.Can any one help why data is not displaying in android. file is existing in external stoage memory in android .but data is not there

Can any one help me resolve this issue.

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/contact_card"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/card_details">

            <Button
                android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Create PDF" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/cards">
                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/slivergray"
                    app:cardCornerRadius="5dp"
                    app:cardElevation="4dp"
                    app:cardUseCompatPadding="true">


                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:background="@color/White">
                        <RelativeLayout
                            android:id="@+id/header"
                            android:layout_width="match_parent"
                            android:layout_height="48dp"
                            android:background="@color/tabs1"
                            android:gravity="center">

                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center_horizontal"
                                android:layout_centerHorizontal="true"
                                android:textAllCaps="true"
                                android:paddingTop="10dp"
                                android:textStyle="bold"
                                android:text="@string/opportunity_details"
                                android:textAppearance="?android:attr/textAppearanceLarge"
                                android:textColor="@color/White"
                                android:textSize="16sp"/>
                            <Button
                                android:id="@+id/close"
                                style="@style/Base.Widget.AppCompat.Button.Borderless"
                                android:layout_width="36dp"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:layout_alignParentTop="true"
                                android:drawableTint="@color/White"
                                android:drawableLeft="@drawable/ic_close_black_24dp"
                                android:textColor="@color/White"
                                android:textSize="12sp"
                                android:textStyle="bold" />

                        </RelativeLayout>
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/header"
                    android:id="@+id/below">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_below="@+id/below"
                        android:id="@+id/main"
                        >
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:id="@+id/sales_stages"
                            android:paddingBottom="10dp"
                            android:paddingTop="10dp"
                            android:weightSum="4"
                            android:layout_below="@+id/main">

                            <TextView
                                android:id="@+id/text_opp"
                                style="@style/Base.TextAppearance.AppCompat.Subhead"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/bill_district"
                                android:paddingLeft="16dp"
                                android:paddingRight="16dp"
                                android:textStyle="bold"
                                android:text="Sales Stage"
                                android:layout_weight="2"
                                android:textColor="@color/gray"
                                android:textSize="15sp"
                                />
                            <TextView
                                android:id="@+id/sales_stage"
                                style="@style/Base.TextAppearance.AppCompat.Subhead"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/text_opp"
                                android:layout_weight="2"
                                android:paddingRight="16dp"
                                android:textColor="@color/gray"
                                android:textSize="15sp"/>
                        </LinearLayout>


                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:id="@+id/suject_ids"
                            android:paddingBottom="10dp"
                            android:layout_below="@+id/sales_stages"
                            android:weightSum="4"
                            android:paddingTop="10dp">



</LinearLayout>

    </FrameLayout>


</RelativeLayout>

java:

create_pdf.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d("size"," "+linearLayout.getWidth() +"  "+linearLayout.getWidth());
                        bitmap = loadBitmapFromView(linearLayout, linearLayout.getWidth(), linearLayout.getHeight());
                        createPdf();
                    }
                });
private void createPdf(){
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
       // Display display = wm.getDefaultDisplay();
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        float hight = displaymetrics.heightPixels ;
        float width = displaymetrics.widthPixels ;

        int convertHighet = (int) hight, convertWidth = (int) width;

   //     Resources mResources = getResources();
        ///Bitmap bitmap = BitmapFactory.decodeResource(mResources, R.drawable.gen_logo);

        PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);

        Canvas canvas = page.getCanvas();



        Paint paint = new Paint();
        canvas.drawPaint(paint);

      //  bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);

        paint.setColor(Color.BLUE);
      //  canvas.drawBitmap(bitmap, 0, 0 , null);

        document.finishPage(page);

        // write the document content
        String targetPdf = "/sdcard/pdffromScroll.pdf";
        File filePath;
        filePath = new File(targetPdf);
        try {
            document.writeTo(new FileOutputStream(filePath));

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
        }
        // close the document
        document.close();
        Toast.makeText(getContext(), "PDF of Scroll is created!!!", Toast.LENGTH_SHORT).show();
        openGeneratedPDF();

    }

    private void openGeneratedPDF(){
        File file = new File("/sdcard/pdffromScroll.pdf");
        if (file.exists())
        {
            Intent intent=new Intent(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try
            {
                startActivity(intent);
            }
            catch(ActivityNotFoundException e)
            {
                Toast.makeText(getContext(), "No Application available to view pdf", Toast.LENGTH_LONG).show();
            }
        }
    }
CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • Why do you use `/sdcard/pdffromScroll.pdf` and not something like `context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.path`? – CoolMind Mar 13 '20 at 17:21
  • https://demonuts.com/android-generate-pdf-view/ following this example –  Mar 15 '20 at 13:23
  • Strange, how it works? Hardcoded paths don't work in Android, as they often change. – CoolMind Mar 15 '20 at 19:04

0 Answers0