Zameer Khan
Zameer Khan

Reputation: 21

How to open PDF viewer(adobe pdf) which in my mobile / emulator

When i click the file which i retrieve from database it will simply downloading. i want to open it from my PDF viewer which in my Mobile

I don't know how and where should i want to change or replace the code

This is the List View

A list view

This is the code which i use to develop

public class ViewFiles extends AppCompatActivity {

    //Variables
    ListView myViewFiles;
    DatabaseReference databaseReference;
    List<String> detail;
    List<uploadFiles> uploadDOCS;

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

        //Displaying the toolbar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //Assigning all the Variables to ID
        myViewFiles = (ListView) findViewById(R.id.myViewFiles);
        uploadDOCS = new ArrayList<uploadFiles>();

        detail =  new ArrayList<>();

        viewAllFiles();

        //On click option for the list view and open it from pdf
        myViewFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                uploadFiles uploadFiles = uploadDOCS.get(position);

                //Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
                Intent intent = new Intent();
                intent.setType(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(uploadFiles.getUrl()));
                startActivity(intent);
            }
        });
    }

    //View files which in databse
    private void viewAllFiles() {

        databaseReference = FirebaseDatabase.getInstance().getReference("HNDIT").child("1st Year 2nd Sem").child("OOP");
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()){

                    uploadFiles uploadFiles = postSnapshot.getValue(com.example.lms.fileupload.uploadFiles.class);
                    uploadDOCS.add(uploadFiles);
                }

                String[] uploads = new String[uploadDOCS.size()];

                for (int i=0; i < uploads.length; i++){
                    uploads[i] = uploadDOCS.get(i).getName();

                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,uploads){
                    @NonNull
                    @Override
                    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                        View view = super.getView(position, convertView, parent);
                        TextView myText = (TextView) view.findViewById(android.R.id.text1);
                        myText.setTextColor(Color.BLACK);

                        return view;
                    }
                };
                myViewFiles.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

how can i fix this

Is there anyway to open inside the app

Please see this too

Upvotes: 0

Views: 57

Answers (2)

cgb_pandey
cgb_pandey

Reputation: 1025

When I click the file which I retrieve from database it will simply start downloading.

Intent.ACTION_VIEW will try to display the pdf file. It requests the system to look for some Activity that is able to open and view this file. That's why it will start downloading. This should be done only if you do not have special Activity in your own app that is capable of opening the file.
Since, in this case, fortunately you do have your own capable Activity so, you should rather launch that Activity with uri of the pdf file as :

Intent intent = new Intent(ViewFiles.this, ViewPdfFiles.class);
//intent.setType(Intent.ACTION_VIEW);
intent.putExtra("url", uploadFiles.getUrl());
startActivity(intent);

Upvotes: 0

Hiren Bharodiya
Hiren Bharodiya

Reputation: 190

https://github.com/barteksc/AndroidPdfViewer Use this library. This is best way to load PDF into Application's screen.

Upvotes: 1

Related Questions