Click here to Skip to main content
14,825,439 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public class MainActivity extends AppCompatActivity {
private ImageView imageview;
private int currentpage =0;
private Button next,previous;
    String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);


        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentpage++;
                render();
            }
        });
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentpage--;
                render();
            }
        });
        render();
    }
private void render(){
        try{

            imageview =(ImageView)findViewById(R.id.image);
            int REQ_WIDTH = imageview.getWidth();
            int REQ_HEIGHT= imageview.getHeight();
            Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH,REQ_HEIGHT,Bitmap.Config.ARGB_4444);

File file = new File(path);
if(file==null){
    Toast.makeText( MainActivity.this,"file not exists",
            Toast.LENGTH_LONG).show();
}else{
    Toast.makeText( MainActivity.this,path,            Toast.LENGTH_LONG).show();
}
            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 m = imageview.getImageMatrix();
            Rect rect=new Rect(0,0,REQ_WIDTH,REQ_HEIGHT );
            renderer.openPage(currentpage).render(bitmap,rect,m,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            imageview.setImageMatrix(m);
            imageview.setImageBitmap(bitmap);
            imageview.invalidate();
        }catch (Exception e){}
}




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

<LinearLayout 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:orientation="vertical"

    tools:context=".MainActivity">
<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
        android:id="@+id/previous"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="previous"/>

        <Button
            android:id="@+id/next"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="next"/>


    </LinearLayout>
</LinearLayout>


What I have tried:

I am using pdfviewer to view pdf files .While using third library and giving above string path, the pdf is showing ,while using simply in built library (like above),it is showing blank screen. The buttons and menu is showing but not pdf file(white screen). Please someone help.
Posted
Updated 11-Aug-20 23:34pm
v2
Comments
David Crow 12-Aug-20 11:02am
   
The variable path has not been assigned a value? Is that intentional?
Vivek Kansal 12-Aug-20 15:03pm
   
path = "/storage/3737-3933/lec3.pdf"; .Sorry this line get missed during copy paste.
David Crow 12-Aug-20 15:12pm
   
Where does this assignment happen? Have you stepped through render() using the debugger (to see if an exception is being thrown)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900