Попробуйте этот код ..
public class MainActivity extends AppCompatActivity {
String LOG_TAG = MainActivity.class.getSimpleName();
Button buttonUpload, buttonDownload;
RadioGroup radioGroup;
ImageView imageviewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonUpload = findViewById(R.id.upload_button);
buttonDownload = findViewById(R.id.download_button);
radioGroup = findViewById(R.id.radio_group);
imageviewResult = findViewById(R.id.resultant_imageview);
buttonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadImage();
}
});
buttonDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadImage();
}
});
}
private void uploadImage() {
// Start by getting our StorageReference
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference rootRef = storage.getReference();
StorageReference bearRef = rootRef.child("images/bear.jpg");
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading");
progressDialog.show();
// Get the data from the image as bytes
ImageView bearImage = getSelectedBearImage();
bearImage.setDrawingCacheEnabled(true);
bearImage.buildDrawingCache();
Bitmap bitmap = bearImage.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
// Upload it to our reference
UploadTask uploadTask = bearRef.putBytes(data);
buttonDownload.setEnabled(false);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
progressDialog.dismiss();
Log.w(LOG_TAG, "Upload failed: " + exception.getMessage());
}
}).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();
progressDialog.dismiss();
Log.d(LOG_TAG, "Download Url: " + downloadUrl);
buttonDownload.setEnabled(true);
}
});
}
private void downloadImage() {
// Start by getting a reference to the same location we uploaded to
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference rootRef = storage.getReference();
StorageReference bearRef = rootRef.child("images/bear.jpg");
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading");
progressDialog.show();
// Download our data with a max allocation of 1MB
final long ONE_MEGABYTE = 1024 * 1024;
bearRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Convert bytes to bitmap and call setImageBitmap
progressDialog.dismiss();
Log.d(LOG_TAG, "Download successful");
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageviewResult.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
progressDialog.dismiss();
Log.w(LOG_TAG, "Download failed: " + exception.getMessage());
}
});
}
private ImageView getSelectedBearImage() {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radio1:
return findViewById(R.id.image_bear1);
case R.id.radio2:
return findViewById(R.id.image_bear2);
case R.id.radio3:
return findViewById(R.id.image_bear3);
case R.id.radio4:
return findViewById(R.id.image_bear4);
default:
return findViewById(R.id.image_bear1);
}
}
}
activity.xml ..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.adruser.firebasestorageapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="96dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_bear1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/a"/>
<ImageView
android:id="@+id/image_bear2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/tiger"/>
<ImageView
android:layout_width="0dp"
android:id="@+id/image_bear3"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/download"/>
<ImageView
android:layout_width="0dp"
android:id="@+id/image_bear4"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/add"/>
</LinearLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloaded Image"/>
<ImageView
android:id="@+id/resultant_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/upload_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Upload"/>
<Button
android:id="@+id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:layout_marginTop="8dp"
android:text="Download"/>
</LinearLayout>
и я надеюсь, что вы подключены к хранилищу Firebase и даете разрешение на доступ в Интернет ..
<uses-permission android:name="android.permission.INTERNET"/>
дополнительную информацию см. По этой ссылке .. https://code.tutsplus.com/tutorials/image-upload-to-firebase-in-android-application--cms-29934