如何在android设备上从WhatsApp视频文件夹上传视频文件到Firebase ?



我想创建一个android应用程序,在那里应该有一种方式在firebase上上传文件。但是,即使在给存储运行时权限后,不知何故视频不能上传。

这是我的MainActivity.java文件代码

public class MainActivity extends AppCompatActivity implements
ActivityCompat.OnRequestPermissionsResultCallback{
private static final int PERMISSION_REQUEST_STORAGE = 0;
private final String[] REQUIRED_PERMISSIONS = new String[]{
"android.permission.WRITE_EXTERNAL_STORAGE",
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.INTERNET"
};
FireBaseHandler fb;
private View mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fb = new FireBaseHandler();
mLayout = findViewById(R.id.main_layout);
// Check if the permission has been granted
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
// Permission is already available, start camera preview
Snackbar.make(mLayout,
"Permission_Available",
Snackbar.LENGTH_SHORT).show();
fb.upload();
} else {
// Permission is missing and must be requested.
requestPermission();
}
/*
if(allPermissionsGranted()){
fb.Upload(); //start upload if permission has been granted by user
} else{
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
}
*/
}
/*
private boolean allPermissionsGranted(){
for(String permission : REQUIRED_PERMISSIONS){
if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
*/
private void requestPermission() {
// Permission has not been granted and must be requested.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// Display a SnackBar with cda button to request the missing permission.
Snackbar.make(mLayout, "camera_access_required",
Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View view) {
// Request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_STORAGE);
}
}).show();
} else {
Snackbar.make(mLayout, "Storage_unavailable", Snackbar.LENGTH_SHORT).show();
// Request the permission. The result will be received in onRequestPermissionResult().
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// BEGIN_INCLUDE(onRequestPermissionsResult)
if (requestCode == PERMISSION_REQUEST_STORAGE) {
// Request for camera permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted. Start camera preview Activity.
Snackbar.make(mLayout, "camera_permission_granted",
Snackbar.LENGTH_SHORT)
.show();
fb.upload();
} else {
// Permission request was denied.
Snackbar.make(mLayout, "Storage_permission_denied",
Snackbar.LENGTH_SHORT)
.show();
}
}
// END_INCLUDE(onRequestPermissionsResult)
}

/*
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == REQUEST_CODE_PERMISSIONS){
if(allPermissionsGranted()){
fb.Upload();
} else{
Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show();
this.finish();
}
}
}
*/
}

这里是上传文件的代码

public class FireBaseHandler {
private static final String TAG = "MyFirebase";
FirebaseStorage storage; // = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef; // = storage.getReference();
UploadTask uploadTask;
public FireBaseHandler() {
storage = FirebaseStorage.getInstance();
storageRef = storage.getReference();
}
public void upload(){
Uri file = Uri.fromFile(new File(getWhatsAppVideoDirectory()+"Abc.mp4"));
StorageReference vidRef = storageRef.child("videos/"+file.getLastPathSegment());
uploadTask = vidRef.putFile(file);
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Log.i(TAG,"onFailure");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
Log.i(TAG,"onSuccess");
}
});
}
private String getWhatsAppVideoDirectory() {
String folder_path = (Environment.getExternalStorageDirectory().getAbsolutePath() +
"/WhatsApp/Media/WhatsApp Video/Sent/");
//new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsApp/Media");
return folder_path;
}
}

错误类似于访问被拒绝,请告诉我我在哪里做错了。

我已经运行这段代码使用较低版本的android是6.0.1,它工作得很好。我不知道谷歌是否对更新的android版本有一些改变?

我刚刚发现你没有正确地请求许可;

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_STORAGE);

这里你只是要求读取权限,读取权限总是为真,当你在manifest声明,但在运行时你应该要求WRITE_EXTERNAL_STORAGE。所以上面的代码没有意义,因为它总是成立的。你必须像这样请求许可:

ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_STORAGE);

最新更新