亲爱的
我限制了firebase的存储,以仅允许经过身份验证的用户从firebae控制台读取写入内容,我的所有项目请求均正常工作,除了PDF文件的下载页面,怀疑我没有以正确的方式添加身份验证侦听器,请检查在下面为我的下载适配器和下载活动找到代码:-
//下载活动///
package com.example.boc;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.Objects;
public class pdfDownloader extends AppCompatActivity {
FirebaseFirestore db;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListner;
FirebaseUser mUser;
RecyclerView mRecyclerView;
ArrayList<DownModel> downModelArrayList = new ArrayList<>();
MyPdfAdapter myAdapter;
public static final String TAG="LOGIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_downloader);
mAuth = FirebaseAuth.getInstance();
mUser = FirebaseAuth.getInstance().getCurrentUser();
mAuthListner = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (mUser != null) {
dataFromFirebase();
}
else
{
Log.d(TAG,"AuthStateChanged:Logout");
}
}
};
setUpRV();
setUpFB();
dataFromFirebase();
}
// allowing users to see the links ////
protected void onStart() {
super.onStart();
if (mUser == null) {
dataFromFirebase();
}
else
{
Log.d(TAG,"AuthStateChanged:Logout");
}
}
private void dataFromFirebase() {
if(downModelArrayList.size()>0)
downModelArrayList.clear();
db.collection("files")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot documentSnapshot: Objects.requireNonNull( task.getResult() )) {
DownModel downModel= new DownModel(documentSnapshot.getString("name"),
documentSnapshot.getString("link"));
downModelArrayList.add(downModel);
}
myAdapter= new MyPdfAdapter(pdfDownloader.this,downModelArrayList);
mRecyclerView.setAdapter(myAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(pdfDownloader.this, "Error ;-.-;", Toast.LENGTH_SHORT).show();
}
})
;
}
private void setUpFB(){
db=FirebaseFirestore.getInstance();
}
private void setUpRV(){
mRecyclerView= findViewById(R.id.recycle);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
////下载适配器///
package com.example.boc;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.ArrayList;
import static android.os.Environment.DIRECTORY_DOWNLOADS;
public class MyPdfAdapter extends RecyclerView.Adapter<MyViewHolder> {
pdfDownloader pdfDownloader;
ArrayList<DownModel> downModels;
FirebaseFirestore db;
public MyPdfAdapter(pdfDownloader pdfDownloader , ArrayList<DownModel> downModels) {
this.pdfDownloader = pdfDownloader;
this.downModels = downModels;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(pdfDownloader.getBaseContext());
View view = layoutInflater.inflate(R.layout.elements, null, false);
return new MyViewHolder(view);
}
@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( );
}
} );
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() {
db.collection( "files" ).document()
.delete()
.addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
} );
}
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);
}
@Override
public int getItemCount() {
return downModels.size();
}
}
答案 0 :(得分:0)
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null; }
}
}
这解决了我的疑问