Forgivee
Newbie
Since firebase documentation is not for begginers, sometimes it can be hard and complex for beginners.
I want to upload image to storage of firebase. I read their documentation and tried but it gives NullPointerException.
I think this null pointer exception is about imageview. There is a abc.jpg image in the drawable folder.
There is no maintains.jpg file on the firebase side. Should there be maintains.jpg file on the firebase? I couldn't also understand here
Activity_main.xml:
Main Activity:
Finally, How to solve?
I want to upload image to storage of firebase. I read their documentation and tried but it gives NullPointerException.
I think this null pointer exception is about imageview. There is a abc.jpg image in the drawable folder.
There is no maintains.jpg file on the firebase side. Should there be maintains.jpg file on the firebase? I couldn't also understand here
Activity_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="gc.imageuploader.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:src="@drawable/abc"/>
</LinearLayout>
Main Activity:
Code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("(Mybucketnamehere)");
StorageReference mountainsRef = storageRef.child("mountains.jpg");
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//Error line
byte[] data = baos.toByteArray();
UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
}