向大家致意,我有这个活动将PDF文件上传到Firebase存储以及将数据保存到Firebase存储中,但是我无法将按钮上载转换为方法UploadFile,没有人知道如何使它工作?
以下几行包含以下错误:-
按钮上传---- 方法UploadFile ---
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
public class UploadActivity extends AppCompatActivity {
private StorageReference mStorageRef;
Button upload;
Button select;
int CODE=215;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
mStorageRef = FirebaseStorage.getInstance().getReference();
upload=findViewById(R.id.upload);
select=findViewById(R.id.select);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectFile();
}
});
upload.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
UploadFile( );//when i put Uri file it says Cannot resolve symbol 'file' //
//and when i remove Uri file from the UploadFile method below then i will get error casted on null //
}
} );
}
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);
}
public void UploadFile(Uri file)
{
Toast.makeText(this, "Please wait.. the file is uploading!", Toast.LENGTH_SHORT).show();
//Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
StorageReference riversRef = mStorageRef.child(file.getLastPathSegment());
riversRef.putFile(file)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(UploadActivity.this, "Upload Success", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(UploadActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
选中https://developer.android.com/guide/topics/providers/document-provider。
编辑2:
清理答案。
您缺少从onActivityResult方法的结果中持久获取数据的信息。 uri需要存储在一个可以将其传递到上传按钮的onClickListener的地方(例如,UploadActivity对象的字段)。
您的onActivityResult方法中已经有Uri SelectedFileLocation
,并在那里进行了上载。为什么还需要另一个按钮?
假设您手动转换为URI的方式与默认方法(Intent#getData
)相同,只需将该变量更改为UploadActivity的字段即可。
不要忘记将not null检查添加到您的UploadFile方法中,或者确保您永远不要使用无效值对其进行调用。
可能看起来像这样:
public class UploadActivity extends AppCompatActivity {
...
Optional<Uri> selectedFile = Optional.empty();
@Override
protected void onCreate(Bundle savedInstanceState) {
...
upload.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedFile.isEmpty() == false) {
uploadFile( selectedFile.get() );
}
}
} );
}
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
...
if (data != null) {
selectedFile = Optional.of(data.getData());
uploadFile(selectedFile.get()); // if you still want to do it here as well
} else {
selectedFile = Optional.empty();
}
...
}
...
}
备注:
还要看一下Java代码约定。
方法和变量应以小写字母开头。
(并且总是将@Override保留在您重写的所有方法上)