我有一个应用程序,可读取数据并输入到存储在**/Internal storage/android/data/files**中的Excel文件中。我想将包含Excel文档的应用程序文件发送到Firebase云。
当执行下面的代码?我得到以下错误。无法检索文件大小是主要错误。一些与应用程序文件路径
public class MainFragment extends Fragment {
FirebaseStorage storage = FirebaseStorage.getInstance();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
//save data to local storage
binding.ivQr.setOnClickListener(view1 -> {
//initiate alertDiolog
new AlertDialog.Builder(requireContext())
.setTitle("Save")
.setMessage("Data has been saved in to local storage of the phone")
.setPositiveButton("OK",(dialog, whichButton) -> {
// TODO: send or do something with data
uploadDataToStorage();
}).show();
});
}
private void uploadDataToStorage(){
StorageReference storageRef = storage.getReference();
Uri file = Uri.fromFile(new File("Internal storage/Android/data/com.example.app/files"));
StorageReference excelFiles = storageRef.child("com.example.app/"+file.getLastPathSegment());
excelFiles.putFile(file);
// handle uploadTask
}
}
误差
could not retrieve file size for upload file:///Internal storage/Android/data/com.example.app/files
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:342)
at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:229)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1844)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1682)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1629)
at com.google.firebase.storage.UploadTask.<init>(UploadTask.java:139)
at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:241)
at com.example.scanqrapp.ui.main.MainFragment.uploadDataToStorage(MainFragment.java:165)
at com.example.scanqrapp.ui.main.MainFragment.lambda$onViewCreated$0$com-example-scanqrapp-ui-main-MainFragment(MainFragment.java:93)
at com.example.scanqrapp.ui.main.MainFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:188)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
could not locate file for uploading:file:///Internal storage/Android/data/com.example.app/files
1条答案
按热度按时间mwkjh3gx1#
排队
...您尝试使用硬编码路径手动构建应用内部数据目录的位置。您不能这样做-不同设备上的实际路径会有很大不同。(顺便说一句,我不知道任何Android设备的内部存储目录路径与您的类似)。
而使用
Context.getFilesDir()
,它会提供应用作用域内部存储目录的位置,您可以从那里开始工作。