Как конвертировать макет прокрутки для сохранения в телефоне и просмотра в формате PDF - PullRequest
0 голосов
/ 01 ноября 2019

У меня есть этот макет прокрутки, который отображает текстовые изображения и изображение из формы ввода пользователя. У меня есть кнопка внутри макета, которая при нажатии пользователем макет прокрутки сохраняет и просматривает в формате PDF. Я пробовал много примеров в интернете, но не удалось. Это потому, что мне нужно создать разрешение пользователя, а затем я не знаю, как правильно реализовать коды, которые приведут к конвертации макета в pdf и сохранению в телефоне. Моя целевая версия SDK - 29.

XML-файл макета:

   <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llPdf"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv9"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:baselineAligned="false"
        android:fontFamily="@font/adamina"
        android:text="@string/propentest_report"
        android:textColor="#084D0B"
        android:textSize="20sp" />


    <TextView
        android:id="@+id/company_name"
        android:layout_width="125dp"
        android:layout_height="31dp"
        android:layout_gravity="center"
        android:fontFamily="sans-serif-black"
        android:lineSpacingExtra="8sp"
        android:text=""
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:lineSpacingExtra="8sp"
        android:text=""
        android:textSize="12sp" />

    <TextView
        android:id="@+id/base"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity=""
        android:layout_marginTop="20dp"
        android:fontFamily="serif"
        android:lineSpacingExtra="8sp"
        android:text="      Base Vulnerability Class:"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/basevultext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text="      "
        android:textSize="14sp" />

    <TextView
        android:id="@+id/asset"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:fontFamily="serif"
        android:lineSpacingExtra="8sp"
        android:text="      Affected Asset:"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/assettext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text="      "
        android:textSize="14sp" />


    <TextView
        android:id="@+id/likeli"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:fontFamily="serif"
        android:lineSpacingExtra="8sp"
        android:text="      Likelihood of exploitation : "
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/likelitext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text="      "
        android:textSize="14sp" />

    <TextView
        android:id="@+id/priority"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:fontFamily="serif"
        android:lineSpacingExtra="8sp"
        android:text="      Priority to remediate :  "
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/priotext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text="      "
        android:textSize="14sp" />

    <TextView
        android:id="@+id/reproduce"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:fontFamily="serif"
        android:lineSpacingExtra="8sp"
        android:text="      The steps to reproduce vulnerability: "
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/reprotext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center|left"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text="      "
        android:textSize="14sp" />


    <TextView
        android:id="@+id/note"
        android:layout_width="412dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:background="#74635151"
        android:lineSpacingExtra="8sp"
        android:text="*NOTES* "
        android:textAlignment="center"
        android:textColor="#C62828"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/notetext"
        android:layout_width="wrap_content"
        android:layout_height="29dp"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:text=""
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv15"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:baselineAligned="false"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="8sp"
        android:fontFamily="@font/adamina"
        android:text="@string/EVIDENCE"
        android:textColor="#084D0B"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="315dp"
        android:layout_height="337dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:background="@drawable/image_border"
        android:contentDescription="@string/todo"
        android:lineSpacingExtra="8sp"
        android:padding="1dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_image_black_24dp" />

    <Button
        android:id="@+id/uploadImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:lineSpacingExtra="8sp"
        android:onClick="chooseImage"
        android:text="@string/upload_image" />
    <Button
        android:id="@+id/pdf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:lineSpacingExtra="3dp"
        android:onClick="savePdf"
        android:text="@string/Create_PDF" />

      </LinearLayout>
      </ScrollView>

Это мой Java-файл:

       package com.example.ProPentest;

       import android.content.Intent;
       import android.graphics.Bitmap;
       import android.net.Uri;
       import android.os.Bundle;
       import android.provider.MediaStore;
       import android.view.View;
       import android.widget.Button;
       import android.widget.ImageView;
       import android.widget.LinearLayout;
       import android.widget.TextView;
       import androidx.annotation.Nullable;
       import androidx.appcompat.app.AppCompatActivity;
        import java.io.IOException;

  public class display extends AppCompatActivity {

   TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8;
   String st, st1, st2, st3, st4, st5, st6, st7;

   ImageView iv;
   private int PICK_IMAGE_REQUEST = 1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.activity_display );


    tv1 = findViewById ( R.id.company_name );
    tv2 = findViewById ( R.id.date );
    tv3 = findViewById ( R.id.assettext );
    tv4 = findViewById ( R.id.reprotext );
    tv5 = findViewById ( R.id.notetext );
    tv6 = findViewById ( R.id.basevultext );
    tv7 = findViewById ( R.id.likelitext );
    tv8 = findViewById ( R.id.priotext );
    iv = findViewById ( R.id.imageView2 );


    st = getIntent ( ).getExtras ( ).getString ( "Name" );
    tv1.setText ( st );
    st1 = getIntent ( ).getExtras ( ).getString ( "Date" );
    tv2.setText ( st1 );
    st2 = getIntent ( ).getExtras ( ).getString ( "Asset" );
    tv3.setText ( st2 );
    st3 = getIntent ( ).getExtras ( ).getString ( "Repro" );
    tv4.setText ( st3 );
    st4 = getIntent ( ).getExtras ( ).getString ( "Note" );
    tv5.setText ( st4 );
    st5 = getIntent ( ).getExtras ( ).getString ( "Spinner" );
    tv6.setText ( st5 );
    st6 = getIntent ( ).getExtras ( ).getString ( "Spinner2" );
    tv7.setText ( st6 );
    st7 = getIntent ( ).getExtras ( ).getString ( "Spinner3" );
    tv8.setText ( st7 );
    //bmp = getIntent().getExtras().getParcelable("imageView2");
    //iv.setImageBitmap( bmp );

    Button selectImage = findViewById ( R.id.uploadImage );

    selectImage.setOnClickListener ( new View.OnClickListener ( ) {


        @Override
        public void onClick(View v) {

            chooseImage ( );
            bmp = iv.getDrawingCache ( );
            Intent i = new Intent ( );
            i.putExtra ( "imageView2" , bmp );

        }

        private void chooseImage() {
            Intent intent = new Intent ( );
            intent.setType ( "image/*" );
            intent.setAction ( Intent.ACTION_GET_CONTENT );
            startActivityForResult ( Intent.createChooser ( intent , "Select Picture" ) ,
                    PICK_IMAGE_REQUEST );
            }


           }
           );
         }


      protected void onActivityResult(int requestCode , int resultCode , @Nullable Intent data) {
           super.onActivityResult ( requestCode , resultCode , data );

           if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData ( ) != null) {

             Uri uri = data.getData ( );

            try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap ( getContentResolver ( ) , uri );
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = findViewById ( R.id.imageView2 );
            imageView.setImageBitmap ( bitmap );
            //  Intent intent;
            //  intent = new Intent(form2.this,display.class);
            //  intent.putExtra("imageUri", uri.toString());
            //  startActivity(intent);
        } catch (IOException e) {
            e.printStackTrace ( );
        }}}}

Это будет честью, если выможет дать мне четкие решения и руководство для достижения результата. Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...