I want to create android application where should be a way to upload file on firebase. But Even after giving runtime permissions to storage, somehow Video is not being uploaded.
Here is my MainActivity.java file code
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();
}
}
}
*/
}
And here is code for uploading file
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;
}
}
Error is something like Access denied, Kindly tell me where am I doing it wrong way.
I have ran this code using Lower version of android that is 6.0.1 and it is working fine. I don't know if Google has some changes for newer android versions?