[英]Unable to Store the image for the second time in to the firebase storage
I created a simple application in which the user can browse through his/her gallery and after selecting the image option for cropping the image appears for that I have used some image crop library which was available on GitHub. All works fine I am able to crop the image and upload it after that image appears into the fire base storage, but when I try to upload the image for the second time with different image it doesn't get uploaded in the firebase storage and only the first uploaded image appears.我创建了一个简单的应用程序,用户可以在其中浏览他/她的图库,在选择用于裁剪图像的图像选项后,我使用了一些在 GitHub 上可用的图像裁剪库。一切正常,我可以裁剪图像并在该图像出现在 fire base 存储中后上传它,但是当我尝试第二次使用不同的图像上传图像时,它不会上传到 firebase 存储中,只有第一个上传的图像出现。 Please help me anyone to do something with this code so that it could upload multiple images
请帮助我任何人使用此代码做一些事情,以便它可以上传多张图片
XML Layout for MainActivity.java XML MainActivity.java 布局
<?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="imgcrop.example.com.androidimagecrop.MainActivity">
<ImageButton
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:scaleType="centerCrop"
android:src="@android:drawable/ic_menu_report_image"
android:visibility="visible" />
<Button
android:id="@+id/uploadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UPLOAD IMAGE"
android:layout_marginTop="10dp"
/>
</LinearLayout>
MainActivity.java MainActivity.java
package imgcrop.example.com.androidimagecrop;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import com.firebase.client.Firebase;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
private Button uploadButton;
private static final int Image_request=1;
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://androidimagecrop.appspot.com");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
//getting the reference of the views
imageButton = (ImageButton) findViewById(R.id.imageview);
uploadButton = (Button) findViewById(R.id.uploadbutton);
onImageViewClick(); // for selecting an Image from gallery.
onUploadButtonClick(); // for uploading the image to Firebase Storage.
}
protected void onImageViewClick(){
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,Image_request );
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Image_request && resultCode == RESULT_OK){
Uri selectedImageUri = data.getData();
CropImage.activity(selectedImageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
imageButton.setImageURI(selectedImageUri);
}
}
if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
CropImage.ActivityResult result= CropImage.getActivityResult(data);
if(resultCode==RESULT_OK)
{
Uri resulturi=result.getUri();
imageButton.setImageURI(resulturi);
StorageReference filepath= storageRef.child(resulturi.getLastPathSegment());
}
else
{
if(resultCode==CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
Exception error=result.getError();
}
}
}
}
private String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
protected void onUploadButtonClick(){
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
storageRef = FirebaseStorage.getInstance().getReference().child("Profiles/image");
imageButton.setDrawingCacheEnabled(true);
imageButton.buildDrawingCache();
Bitmap bitmap = imageButton.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = storageRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(MainActivity.this, "TASK FAILED", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Image Uploaded SucessFully", Toast.LENGTH_SHORT).show();
Uri downloadUrl =taskSnapshot.getDownloadUrl();
String DOWNLOAD_URL = downloadUrl.getPath();
}
});
}
});
}
}
But on changing the name of child reference which is in onUploadButtonClick() method, I am able to store different image但是在更改 onUploadButtonClick() 方法中的子引用的名称时,我能够存储不同的图像
Here is my firebase storage section.这是我的 firebase 存储部分。 I have created a root folder under which all images should get stored
我创建了一个根文件夹,所有图像都应存储在该文件夹下
replace storageRef = FirebaseStorage.getInstance().getReference().child("Profiles/image");
替换
storageRef = FirebaseStorage.getInstance().getReference().child("Profiles/image");
with replace storageRef = FirebaseStorage.getInstance().getReference().child("Profiles/"+System.currentTimeMillis());
用replace
storageRef = FirebaseStorage.getInstance().getReference().child("Profiles/"+System.currentTimeMillis());
Just provide different reference each time.只是每次提供不同的参考。 For example use timestamp.
例如使用时间戳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.