Reputation: 63
I am making an app that lets the user upload PDF files and then I save them to Firebase. Now I'm trying to display them in my app, I don't want to let the user download the file, but I want it to be displayed directly in the app.
This is how I save the files:
private void uploadFile() {
progressBar.setVisibility(View.VISIBLE);
StorageReference sRef = mStorageReference.child(Constants.STORAGE_PATH_COURSES + System.currentTimeMillis() + ".pdf");
sRef.putFile(filepath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@SuppressWarnings("VisibleForTests")
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressBar.setVisibility(View.GONE);
textViewStatus.setText("File Uploaded Successfully");
Course upload = new Course(editTextFilename.getText().toString(), taskSnapshot.getDownloadUrl().toString());
mDatabaseReference.child(mDatabaseReference.push().getKey()).setValue(upload);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@SuppressWarnings("VisibleForTests")
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
textViewStatus.setText((int) progress + "% Uploading...");
}
});
}
I searched for the possibilities and tried a few but I can not make them work.
I tried to use this, but it displays nothing. https://github.com/barteksc/AndroidPdfViewer
pdfView=(PDFView) findViewById(R.id.pdfView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
url = getIntent().getStringExtra("url");
}
uri=Uri.parse(url);
pdfView.fromUri(uri).load();
If I load the pdf from the assets folder, it works well.
I've also tried the webview:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + url)
I'm really new to Android and I'm sure that I do something wrong...I would be really grateful if someone could help me. Thanks!
Upvotes: 3
Views: 10307
Reputation: 763
after searching and retry different codes i found the solution . view pdf file without downloading in device
Library for pdf view is
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Add in XML
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfview"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and This is java code
StorageReference storageRef;
FirebaseApp app;
FirebaseStorage storage;
storageRef = FirebaseStorage.getInstance().getReference();
app = FirebaseApp.getInstance();
storage = FirebaseStorage.getInstance(app);
storageRef = storage.getReference().child("books/booksPDF/documenttcv.pdf");
storageRef.getStream().addOnSuccessListener(new OnSuccessListener<StreamDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(StreamDownloadTask.TaskSnapshot taskSnapshot) {
pdfview.fromStream(taskSnapshot.getStream()).load();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ReadBookActivity.this, "Fail :"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
NOTE : When you save pdf file programaticaly in firbase storage then you need to keep the name of file insted of url , because you retrieve files by using name no url
Upvotes: 1
Reputation: 31
This one works, too.
This is from Firebase Doc website:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Write a message to the database
pdfView = (PDFView) findViewById(R.id.pdfView);
mFirebaseStorage=FirebaseStorage.getInstance();
mmFirebaseStorageRef=mFirebaseStorage.getReference().child("sources");
final long ONE_MEGABYTE = 1024 * 1024;
mmFirebaseStorageRef.child("smpl.pdf").getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
pdfView.fromBytes(bytes).load();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this,"download unsuccessful",Toast.LENGTH_LONG).show();
}
});
}
Upvotes: 1
Reputation: 63
I finally succeeded using PDFViewer. Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
pdfView=(PDFView) findViewById(R.id.pdfView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
url = getIntent().getStringExtra("url");
}
new RetrievePDFStream().execute(url);
}
class RetrievePDFStream extends AsyncTask<String,Void,InputStream>{
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream=null;
try{
URL urlx=new URL(strings[0]);
HttpURLConnection urlConnection=(HttpURLConnection) urlx.openConnection();
if(urlConnection.getResponseCode()==200){
inputStream=new BufferedInputStream(urlConnection.getInputStream());
}
}catch (IOException e){
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}
This really helped: https://www.youtube.com/watch?v=-Ld1IoOF_uk
Upvotes: 3
Reputation: 3869
I'm not using Firebase but my PDF is stored on a server. I call the server using Retrofit to download the PDF as a Stream. Then I give the Stream to pdf Viewer
To display the PDF
private void displayPDF(InputStream stream) {
mPdfView.fromStream(stream)
.defaultPage(0)
.load();
}
Retrofit Service
@Streaming
@GET
Call<ResponseBody> downloadPDF(@Url String url);
In Retrofit response
displayPDF(responseBody.byteStream());
Upvotes: 1
Reputation: 5370
AndroidPdfViewer
library supports loading PDF file from local storage not from remote url.Since your are trying to load remote link it wont show anything.You need to download your pdf file in app cache and load it from that path
WebView
will work fine in this situation because its loading a remote url in a browser
You can refer the following Issue HERE
Upvotes: 2