У меня есть коды ниже, которые загружают и удаляют файлы из списка пожарного магазина (фактически это удаляет данные в документе пожарного магазина, а не реальный файл в хранилище)
Правильно ли я называю имя файла, который я сохранил в сеансе загрузки?
почему я не могу привести метод delete к созданной мной кнопке
пожалуйста, найдите коды: -
//my adapater has the delete and download methods //
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.mName.setText(downModels.get(i).getName());
myViewHolder.mLink.setText(downModels.get(i).getLink());
myViewHolder.deletePdfBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deletepdf(final int i); //here i got an error says Unexpected token//
}
});
myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile(myViewHolder.mName.getContext(), downModels.get(i).getName(), ".pdf", DIRECTORY_DOWNLOADS, downModels.get(i).getLink());
}
});
}
private void deletepdf(final int i) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
String filenamed = downModels.get(i).get_name(); // am i calling the write parameter (i want to call the file name as i uploaded the document name just same to it from text view in the upload page //
DocumentReference documentReference = db.collection("files").document(filenamed);
Map < String, Object > Updates = new HashMap < > ();
Updates.put("link", FieldValue.delete());
documentReference.update(Updates)
.addOnSuccessListener(new OnSuccessListener < Void > () {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
}
public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadmanager = (DownloadManager) context.
getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
downloadmanager.enqueue(request);
}
// и вот мое действие по загрузке //
package com.example.boc.Pdf;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.boc.ImageView.MyPdfAdapter;
import com.example.boc.R;
import com.example.boc.main.MainActivity;
import com.example.boc.models.DownModel;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public class pdfUploader extends AppCompatActivity {
private StorageReference mStorageRef;
private FirebaseFirestore db;
private static final String NAME_KEY = "name";
Button upload;
String stringame;
EditText putfilenameTxt;
Button select;
int CODE = 215;
private Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = FirebaseFirestore.getInstance();
setContentView(R.layout.activity_pdf_uploader);
mStorageRef = FirebaseStorage.getInstance().getReference("pdf");
upload = findViewById(R.id.upload);
putfilenameTxt = findViewById(R.id.putfilenameTxt);
context = this;
select = findViewById(R.id.choose);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectFile();
}
});
}
public void selectFile() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "Select a file"), CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String filePath = data.getDataString();
Uri SelectedFileLocation = Uri.parse(filePath);
UploadFile(SelectedFileLocation);
super.onActivityResult(requestCode, resultCode, data);
}
private static final String TAG = "MainActivity";
public void UploadFile(final Uri file) {
final String nameoffile = putfilenameTxt.getText().toString();
Toast.makeText(this, "Please wait.. the file is uploading!", Toast.LENGTH_SHORT).show();
//Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
final StorageReference riversRef = mStorageRef.child(System.currentTimeMillis() + ".");
riversRef.putFile(file).addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
riversRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener < Uri > () {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
Toast.makeText(pdfUploader.this, "Upload Done", Toast.LENGTH_LONG).show();
//After upload Complete we have to store the Data to firestore.
Map < String, Object > file = new HashMap < > ();
file.put(NAME_KEY, nameoffile);
file.put("link", downloadUrl.toString()); // We are using it as String because our data type in Firestore will be String
db.collection("files").document(nameoffile)
.set(file, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener < Void > () {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
}
});
}
});
}
}
// Загрузить МОДЕЛЬ //
package com.example.boc.models;
public class DownModel {
String Name,Link;
private String name,link;
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getLink() {
return Link;
}
public void setLink(String link) { this.Link = link; }
public String get_name() {
return name;
}
public DownModel(String Name, String Link){
this.Link=Link;
this.Name=Name;
this.name = name;
}
}
структура базы огня
хранилище