How to Upload Images to Android Studio

How-do-you-do Developers,

In this post let'south encounter how we can upload and retrieve images to Firebase storage using Android studio. Earlier starting with this, please see the video below and so every bit to prepare your Google Firebase project for this tutorial.

Download Source code.

We will accept to-

  1. Turn ON Google Firebase Authentication and add a new user from console to login in our Android awarding.
  2. Turn ON Google Firebase Storage so that we can upload and think images uploaded from our Android Awarding.
  3. Add together this features to our Android Project using Firebase assistant provided in Android studio.

Okay. Permit's start creating the application.
This awarding will be having a simple Firebase authentication Login screen. For the offset time users will have to login into the app and next fourth dimension the firebase auth will check for the user and straight move to the master page, where one can upload images on Firebase and recollect it back using the epitome url.

Create a new layout file with proper name login.xml in your res layout folder and edit it equally below:

res > layout > login.xml:  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp">  <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Email:" android:textColor="#000" />  <EditText android:id="@+id/edtEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:hint="Email Accost" android:textColor="#000" />  <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Countersign:" android:textColor="#000" />  <EditText android:id="@+id/edtPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:hint="Password" android:inputType="textPassword" android:textColor="#000" />  <Push android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_margin="2dp" android:groundwork="@android:drawable/btn_default" android:text="Login" /> </LinearLayout>        

This will be our Login class.

Edit activity_main.xml file as beneath:

res>layout>activity_main.xml

<?xml version="ane.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:padding="5dp" android:layout_height="match_parent" android:orientation="vertical">  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Image to Upload" android:textColor="#000" />  <ImageView android:id="@+id/imgSource" android:layout_width="200dp" android:layout_height="200dp" android:layout_margin="2dp" />  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Downloaded Paradigm" android:textColor="#000" />  <ImageView android:id="@+id/img" android:layout_width="200dp" android:layout_height="200dp" android:layout_margin="2dp" />  <LinearLayout android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">  <Button android:id="@+id/btnPickImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@android:drawable/btn_default_small" android:text="Pick Paradigm" />  <Button android:id="@+id/btnUpload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:groundwork="@android:drawable/btn_default_small" android:text="Upload" /> </LinearLayout>  <ProgressBar android:id="@+id/pbbar" android:layout_width="wrap_content" android:layout_height="wrap_content" />  </LinearLayout>        

This volition be design of this layout:

Now let's lawmaking this Android application.

Make a new coffee file with name Login.java and edit it equally below:

Login.java:

package com.my.firebaseimage;  import android.content.Intent; import android.os.Bundle; import android.support.notation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;  import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser;  public form Login extends AppCompatActivity {  private FirebaseAuth mAuth; EditText edtEmail, edtPassword; Button btnLogin; FirebaseUser user;  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login);  mAuth = FirebaseAuth.getInstance(); user = mAuth.getCurrentUser(); if (user == null) Toast.makeText(Login.this, "First time user", Toast.LENGTH_SHORT).show(); else startActivity(new Intent(Login.this, MainActivity.grade)); edtEmail = findViewById(R.id.edtEmail); edtPassword = findViewById(R.id.edtPassword);  btnLogin = findViewById(R.id.btnLogin);  btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAuth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString()) .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Job<AuthResult> task) { if (task.isSuccessful()) { user = mAuth.getCurrentUser(); Intent i = new Intent(Login.this, MainActivity.form); startActivity(i); } else { Toast.makeText(Login.this, "Authentication failed.", Toast.LENGTH_SHORT).evidence(); } } }); } });  }  @Override public void onStart() { super.onStart(); user = mAuth.getCurrentUser(); } }        

When the application starts for the very first time, users have to login in the application. After that users will exist directly directed to the activity where they can upload Images to Google Firebase.

And edit your MainActivity.java file equally below:
MainActivity.java

package com.my.firebaseimage;  import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.AsyncTask; import android.bone.Bundle; import android.os.Environment; import android.back up.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Push; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.Toast;  import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.storage.FirebaseStorage; import com.google.firebase.storage.StorageReference; import com.google.firebase.storage.UploadTask;  import java.io.FileNotFoundException; import java.io.InputStream; import coffee.nio.charset.MalformedInputException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Agenda;  public class MainActivity extends AppCompatActivity { private StorageReference mStorageRef;  Button btnPickImage, btnUpload; ImageView imgSource, imgDestination; LinearLayout lv; Uri selectedImage; ProgressBar pbbar; DatabaseReference databaseReference, childReference; private static last int REQUEST_TAKE_GALLERY_PHOTO = 2; StorageReference fireRef; private FirebaseAuth mAuth; FirebaseUser currentUser; String imageURL = "";  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  mAuth = FirebaseAuth.getInstance(); currentUser = mAuth.getCurrentUser();  pbbar = findViewById(R.id.pbbar); pbbar.setVisibility(View.GONE);  lv = findViewById(R.id.lv); mStorageRef = FirebaseStorage.getInstance().getReference(); databaseReference = FirebaseDatabase.getInstance().getReference();  btnPickImage = findViewById(R.id.btnPickImage); btnUpload = findViewById(R.id.btnUpload);  imgSource = findViewById(R.id.imgSource); imgDestination = findViewById(R.id.img); btnPickImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Surroundings.getExternalStorageState().equals( Environs.MEDIA_MOUNTED) && !Environs.getExternalStorageState().equals( Environment.MEDIA_CHECKING)) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("paradigm/*"); startActivityForResult(intent, REQUEST_TAKE_GALLERY_PHOTO);  } else Toast.makeText(MainActivity.this, "No gallery found.", Toast.LENGTH_SHORT).show(); } });  btnUpload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View 5) { UploadImages(); } });  }  @Override protected void onActivityResult(int requestCode, int resultCode, Intent information) { super.onActivityResult(requestCode, resultCode, data);  if (resultCode == RESULT_OK) { if (requestCode == REQUEST_TAKE_GALLERY_PHOTO) { Bitmap originBitmap = null; selectedImage = information.getData(); InputStream imageStream; effort { pbbar.setVisibility(View.VISIBLE); imageStream = getContentResolver().openInputStream( selectedImage); originBitmap = BitmapFactory.decodeStream(imageStream); } grab (FileNotFoundException e) { east.printStackTrace(); } if (originBitmap != null) { { this.imgSource.setImageBitmap(originBitmap); pbbar.setVisibility(View.GONE); imgSource.setVisibility(View.VISIBLE); } } else selectedImage = null; }  } }  public Cord GetDate() { DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String currentdate = df.format(Calendar.getInstance().getTime()); return currentdate; }  public void UploadImages() { try { pbbar.setVisibility(View.VISIBLE); lv.setVisibility(View.GONE);  String strFileName = GetDate() + "img.jpg";  Uri file = selectedImage;  fireRef = mStorageRef.child("images/" + currentUser.getUid().toString() + "/" + strFileName);  UploadTask uploadTask = fireRef.putFile(file); Log.e("Fire Path", fireRef.toString()); Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Chore<Uri>>() { @Override public Chore<Uri> so(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception { if (!job.isSuccessful()) { throw task.getException(); } render fireRef.getDownloadUrl(); } }).addOnCompleteListener(new OnCompleteListener<Uri>() { @Override public void onComplete(@NonNull Task<Uri> task) { if (job.isSuccessful()) { Uri downloadUri = task.getResult(); Log.east("Image URL", downloadUri.toString());  pbbar.setVisibility(View.GONE); selectedImage = zippo; imageURL = downloadUri.toString(); } else { Toast.makeText(MainActivity.this, "Image upload unsuccessful. Please try once more." , Toast.LENGTH_LONG).show(); } pbbar.setVisibility(View.GONE); lv.setVisibility(View.VISIBLE);  DownloadImageFromURL downloadImageFromURL = new DownloadImageFromURL(); downloadImageFromURL.execute(""); } }); } grab (Exception ex) { Toast.makeText(MainActivity.this, ex.getMessage().toString(), Toast.LENGTH_LONG).show(); } }  private class DownloadImageFromURL extends AsyncTask<String, Void, String> { Bitmap bitmap = null;  @Override protected void onPreExecute() {  }  protected String doInBackground(String... urls) { effort { Log.e("imageURL is ", imageURL); InputStream in = new java.internet.URL(imageURL).openStream(); if (in != zero) { bitmap = BitmapFactory.decodeStream(in); } else Log.e("Empty InputStream", "InputStream is empty."); } take hold of (MalformedInputException e) { Log.e("Error URL", e.getMessage().toString()); } catch (Exception ex) { Log.e("Input stream mistake", "Input stream mistake"); } return ""; }  protected void onPostExecute(String result) { if (bitmap != null) { imgDestination.setImageBitmap(bitmap); } else Log.e("Empty Bitmap", "Bitmap is empty."); } }  }        

Afterwards uploading the images to firebase storage, the async method will think the image using the Epitome url from the uploading method.

Too add together permissions to your Android Manisfest file for using Internet or Wifi and Storage:

AndroidManifest.xml:

<?xml version="i.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.firebaseimage">  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  <uses-permission android:proper noun="android.permission.Cyberspace" /> <uses-permission android:proper noun="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:proper noun="android.permission.ACCESS_WIFI_STATE" />  <application android:allowBackup="true" android:icon="@drawable/login" android:label="@string/app_name"  android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Login" android:screenOrientation="portrait"> <intent-filter> <activity android:name="android.intent.action.Primary" />  <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:screenOrientation="portrait"/> </application>  </manifest>        

This way yous can Upload and Retrieve images from Google Firebase on Android.

Download Source code.

Thanks for Reading. Hope you found this post helpful. 🙂

turneyantim1984.blogspot.com

Source: https://parallelcodes.com/android-firebase-upload-and-retrieve-images/

0 Response to "How to Upload Images to Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel