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.