firebase中的多个图像上传(android studio)

时间:2017-07-17 12:33:21

标签: android firebase firebase-realtime-database firebase-storage

我想创建一个可以上传超过1张照片的应用程序,并将imgUrl保存在firebase数据库和firebase存储中。但到目前为止,我还没有找到满足我需求的参考资料。我需要你的帮助。非常感谢。

我已经尝试了一个示例,但在将url上传到firebase数据库和firebase存储时仍然没有成功

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static Button openCustomGallery, btnUploadImage;
private static GridView selectedImageGridView;

private static final int CustomGallerySelectId = 1;//Set Intent Id
public static final String CustomGalleryIntentKey = "ImageArray";//Set Intent Key Value

private StorageReference storageReference;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
    setListeners();
    getSharedImages();
    btnUploadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            upload();
        }
    });
}

//Init all views
private void initViews() {
    openCustomGallery = (Button) findViewById(R.id.openCustomGallery);
    selectedImageGridView = (GridView) findViewById(R.id.selectedImagesGridView);
    btnUploadImage = (Button)findViewById(R.id.btnUploadGambar);
}

//set Listeners
private void setListeners() {
    openCustomGallery.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.openCustomGallery:
            //Start Custom Gallery Activity by passing intent id
            startActivityForResult(new Intent(MainActivity.this, CustomGallery_Activity.class), CustomGallerySelectId);
            break;
    }

}

protected void onActivityResult(int requestcode, int resultcode,
                                Intent imagereturnintent) {
    super.onActivityResult(requestcode, resultcode, imagereturnintent);
    switch (requestcode) {
        case CustomGallerySelectId:
            if (resultcode == RESULT_OK) {
                String imagesArray = imagereturnintent.getStringExtra(CustomGalleryIntentKey);//get Intent data
                //Convert string array into List by splitting by ',' and substring after '[' and before ']'
                List<String> selectedImages = Arrays.asList(imagesArray.substring(1, imagesArray.length() - 1).split(", "));
                loadGridView(new ArrayList<String>(selectedImages));//call load gridview method by passing converted list into arrayList
            }
            break;

    }
}

//Load GridView
private void loadGridView(ArrayList<String> imagesArray) {
    GridView_Adapter adapter = new GridView_Adapter(MainActivity.this, imagesArray, false);
    selectedImageGridView.setAdapter(adapter);
}

//Read Shared Images
private void getSharedImages() {

    //If Intent Action equals then proceed
    if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())
            && getIntent().hasExtra(Intent.EXTRA_STREAM)) {
        ArrayList<Parcelable> list =
                getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);//get Parcelabe list
        ArrayList<String> selectedImages = new ArrayList<>();

        //Loop to all parcelable list
        for (Parcelable parcel : list) {
            Uri uri = (Uri) parcel;//get URI
            String sourcepath = getPath(uri);//Get Path of URI
            selectedImages.add(sourcepath);//add images to arraylist
        }
        loadGridView(selectedImages);//call load gridview
    }
}


//get actual path of uri
public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

private void upload() {
    String[] projection = {MediaStore.Images.Media.DATA};
    StorageReference sRef = storageReference.child(projection);
    sRef.putFile(projection).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(getApplicationContext(), "File uploaded", Toast.LENGTH_SHORT).show();

            Upload upload = new Upload(taskSnapshot.getDownloadUrl().toString());
            String id = mDatabase.push().getKey();
            mDatabase.child(id).setValue(upload);
        }
    });
}
}

Upload.java

public class Upload {
public String<Uri> images = new ArrayList<>();

public Upload(List<Uri> images) {
    this.images = images;
}

public List<Uri> getImages() {
    return images;
}

public void setImages(List<Uri> images) {
    this.images = images;
}

0 个答案:

没有答案