我在 recyclerview 中显示我的文件,目标是能够在单击时将它们作为隐式意图通过电子邮件发送,因此用户不必通过从外部存储中提取它们来手动执行此操作。
但是,当我尝试执行此操作时,我的应用程序崩溃,没有错误消息。这是我获取文件路径并尝试发送它的主要活动,专门执行此操作的代码在 sendAsMail 和 OnItemClick 方法中
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FileView extends AppCompatActivity {
private RecyclerView fileRecyclerView;
private RowAdapter fileAdapter;
private RecyclerView.LayoutManager fileLayoutManager;
private ArrayList<RowItem> rowItem;
private List<File> fileList;
private final String filePath = "PDF_files";
private String fileData = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_view);
File file = new File(getExternalFilesDir(filePath).toString());
fileList = Arrays.asList(file.listFiles());
createRows();
buildRecyclerView();
}
public void createRows(){
rowItem = new ArrayList<>();
for (int i = 0; i < fileList.size(); i++) {
rowItem.add(new RowItem(R.drawable.ic_book,(fileList.get(i).getName().replace("__", " ").replace('_','\n').replace('-','/').replace(".pdf",""))));
}
}
public void removeItem(int position) {
rowItem.remove(position);
fileAdapter.notifyItemRemoved(position);
}
public void reListFiles(){
File file = new File(getExternalFilesDir(filePath).toString());
fileList = Arrays.asList(file.listFiles());
}
public void sendAsMail(String file) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample Subject"); //set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Sample Text"); //set your message
String filePath = file;
File fileToShare = new File(filePath);
Uri uri = Uri.fromFile(fileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share File"));
}
public void buildRecyclerView() {
fileRecyclerView = findViewById(R.id.recyclerView);
fileRecyclerView.setHasFixedSize(true);
fileLayoutManager = new LinearLayoutManager(this);
fileAdapter = new RowAdapter(rowItem);
fileRecyclerView.setLayoutManager(fileLayoutManager);
fileRecyclerView.setAdapter(fileAdapter);
fileAdapter.setOnItemClickListener(new RowAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
fileData = fileList.get(position).toString();
// Toast.makeText(FileView.this,"Clicked: " + fileData , Toast.LENGTH_SHORT).show();
sendAsMail(fileData);
}
@Override
public void onDeleteClick(int position) {
removeItem(position);
File deletePath = fileList.get(position);
deletePath.delete();
if(deletePath.exists()){
getApplicationContext().deleteFile(deletePath.getName());
}
reListFiles();
}
@Override
public void onMenuClick(int position) {
}
});
}
}
用建议的解决方案编辑
我的 sendAsMail 方法
private void sendEmail(File attatchedFile, String name) {
//File pdfFile = attatchedFile;
File pdfFile = attatchedFile;
Log.v("sendEmail STARTED: ","attatched file vaariable saved as File: " + pdfFile);
// Write data in your file
Uri uri = FileProvider.getUriForFile(this, getPackageName(), pdfFile);
Intent intent = ShareCompat.IntentBuilder.from(this)
.setStream(uri) // uri from FileProvider
.getIntent()
.setAction(Intent.ACTION_VIEW) //Change if needed
.setDataAndType(uri, "pdf/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
在onclick中调用方法
fileAdapter.setOnItemClickListener(new RowAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
String name = fileList.get(position).getName();
fileData = fileList.get(position);
//Log.d("LOG: ","fileData: " + fileData + " name: " + name);
sendEmail(fileData, name);
}
我将以下变量传递给 sendEmail 方法
文件数据:/storage/emulated/0/Android/data/com.loopbreakr.firstpdf/files/PDF_files/file__name_2021-01-28_16:20:14.pdf
名称:文件__name_2021-01-28_16:20:14.pdf
编辑 - 我的登录崩溃
2021-01-28 17:08:11.904 19378-19378/com.loopbreakr.firstpdf V/STARTING METHOD W/:: fileData: /storage/emulated/0/Android/data/com.loopbreakr.firstpdf/files/PDF_files/file__name_2021-01-28_16:20:14.pdf name: file__name_2021-01-28_16:20:14.pdf
2021-01-28 17:08:11.904 19378-19378/com.loopbreakr.firstpdf V/sendEmail STARTED:: attatched file vaariable saved as File: /storage/emulated/0/Android/data/com.loopbreakr.firstpdf/files/PDF_files/file__name_2021-01-28_16:20:14.pdf
2021-01-28 17:08:11.906 19378-19378/com.loopbreakr.firstpdf D/AndroidRuntime: Shutting down VM
2021-01-28 17:08:11.907 19378-19378/com.loopbreakr.firstpdf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.loopbreakr.firstpdf, PID: 19378
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.loopbreakr.firstpdf
at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:606)
at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.loopbreakr.firstpdf.FileView.sendEmail(FileView.java:71)
at com.loopbreakr.firstpdf.FileView.access$200(FileView.java:25)
at com.loopbreakr.firstpdf.FileView$1.onItemClick(FileView.java:99)
at com.loopbreakr.firstpdf.RowAdapter$RowViewHolder.lambda$new$0$RowAdapter$RowViewHolder(RowAdapter.java:51)
at com.loopbreakr.firstpdf.-$$Lambda$RowAdapter$RowViewHolder$D8MO8iiQsge5Oqk6qgHk3jkm7ew.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)