/Android Dersleri
MainActivity.java
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private StorageReference mStorageRef;
public void downloadFile(){
File localFile = null;
try {
localFile = File.createTempFile(“images”, “jpg”);
} catch (IOException e) {
e.printStackTrace();
}
mStorageRef.getFile(localFile)
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Successfully downloaded data to local file
// …
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle failed download
// …
}
});
}
public void fileUpload(String fileLocation,String uploadLocation){
///Download/android.png
//images/android.png
Uri file = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+fileLocation));
StorageReference riversRef = mStorageRef.child(uploadLocation);
riversRef.putFile(file)
.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//YÜKLEME İŞLEMİ BAŞARILIYSA
//Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//YÜKLEME İŞLEMİ BAŞARISIZ İSE
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorageRef = FirebaseStorage.getInstance().getReference();
fileUpload(“Download/android.png”,”resimler/android.png”);
}
}
AndroidManifest.xml
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
Gradle (Module: app)
implementation ‘com.google.firebase:firebase-storage:16.0.1’