1.Let those variable
MaterialCardView addPDF;
private final int REQ = 1;
private Uri pdfData;
2.Initialize those variable
addPDF= findViewById(R.id.selectPdf);
3. set OnClickListener
addPDF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGallery();
}
});
4. Create a private void method
private void openGallery() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Pdf File"),REQ);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ && resultCode == RESULT_OK && data != null) {
pdfData = data.getData();
Toast.makeText(this, ""+pdfData, Toast.LENGTH_SHORT).show();
}
}
2nd Part ( Upload PDF in Firebase)
Declaration those variables
MaterialCardView addPDF;
private final int REQ = 1;
private Uri pdfData;
private TextInputEditText pdfTitle;
private MaterialButton btnUploadPDF;
private TextView pdfTextView;
private String pdfName,titlePDF;
private DatabaseReference DtbReference;
private StorageReference StrReference;
String downloadUrl = "";
private ProgressDialog pd;
Initialization those variables
addPDF= findViewById(R.id.selectPdf);
btnUploadPDF= findViewById(R.id.btnUploadPDF);
pdfTitle = findViewById(R.id.pdfTitle);
pdfTextView = findViewById(R.id.pdfTextView);
DtbReference = FirebaseDatabase.getInstance().getReference();
StrReference = FirebaseStorage.getInstance().getReference();
pd = new ProgressDialog(UploadPdf.this);
Create private void Methods
private void openGallery() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Pdf File"),REQ);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ && resultCode == RESULT_OK && data != null) {
pdfData = data.getData();
//-------------------Below the code for Get PDF file name---------------------
if(pdfData.toString().startsWith("content://")){
Cursor cursor = null;
try {
cursor = getContentResolver().query(pdfData, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
pdfName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
if (cursor != null) {
cursor.close();
}
}
} else if (pdfData.toString().startsWith("file://")){
pdfName = new File(pdfData.toString()).getName();
}
pdfTextView.setText(pdfName);
}
}
//=========================PDF Pick From Gallery Code End===============================
private void uploadPdf(){
pd.setTitle("Please Wait...");
pd.setMessage("Uploading Pdf");
pd.show();
StorageReference reference = StrReference.child("pdf/"+pdfName+"-"+System.currentTimeMillis()+".pdf");
reference.putFile(pdfData)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
while (!uriTask.isComplete());
Uri uri = uriTask.getResult();
uploadData(String.valueOf(uri));
pd.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
Toast.makeText(UploadPdf.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
//--------------------------------------
private void uploadData(String valueOf){
String uniqueKey = DtbReference.child("pdf").push().getKey();
HashMap data = new HashMap();
data.put("pTitle", titlePDF);
data.put("pUrl", downloadUrl);
DtbReference.child("pdf").child(uniqueKey).setValue(data)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
pd.dismiss();
pdfTitle.setText("");
Toast.makeText(UploadPdf.this, "PDF Uploaded Successfully", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
Toast.makeText(UploadPdf.this, "Failed to upload PDF", Toast.LENGTH_SHORT).show();
}
});
}
Now Create OnClickListener
addPDF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGallery();
}
});
btnUploadPDF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
titlePDF = pdfTitle.getText().toString();
if (titlePDF.isEmpty()){
pdfTitle.setError("Empty");
pdfTitle.requestFocus();
} else if (pdfData == null){
Toast.makeText(UploadPdf.this, "Please upload pdf", Toast.LENGTH_SHORT).show();
} else {
uploadPdf();
}
}
});
Take this XML code here
<com.google.android.material.card.MaterialCardView
android:id="@+id/selectPdf"
android:layout_width="130dp"
android:layout_height="140dp"
android:layout_gravity="center"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/circle_yellow"
android:src="@drawable/ic_pdf"
android:padding="15dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/lightGray"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select PDF"
android:textStyle="bold"
android:padding="5dp"
android:layout_marginTop="10dp"
android:textColor="@color/textColor"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="@+id/pdfTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="No file selected"
android:textSize="16sp"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_margin="16dp"
app:boxStrokeWidth="2dp"
app:boxStrokeColor="@color/green"
android:hint="PDF Title">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/pdfTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/btnUploadPDF"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="25sp"
android:layout_marginRight="25sp"
android:text="Upload PDF"
android:textStyle="bold"
android:textSize="20sp"
/>
Post a Comment