Android没有选择图片将其上传到Fire base

时间:2018-02-02 17:58:27

标签: java android android-layout firebase firebase-storage

我正在开发一个在fire base服务器上创建用户配置文件的项目,但当我点击应用程序中的select按钮时,它会打开图库,但不会选择图像来上传,而是显示图像,但无法选择上传那些图像开火基地

ImageView imageView;
EditText Createname,CreateEmail;
StorageReference storageReference;
private DatabaseReference databaseReference;
public static final String STORAGE_PATH = "images/";
public static final String DATABASE_PATH = "mainObject";
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);
    imageView = findViewById(R.id.insertImages);
    Createname= findViewById(R.id.insertname);
    CreateEmail = findViewById(R.id.insertEmail);
    storageReference = FirebaseStorage.getInstance().getReference();
    databaseReference = FirebaseDatabase.getInstance().getReference(DATABASE_PATH);
}
public void browseImages(View view){
    Intent intent = new Intent();
    intent.setType("images/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Image"),0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==0 && requestCode == RESULT_OK){
     imageUri = data.getData();
     try {
         Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri);
         imageView.setImageBitmap(bitmap);
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
    }
}
public String getActualImage(Uri uri){
    ContentResolver contentResolver = getContentResolver();
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));

}

public void uploadData(View view){
    if (imageUri != null){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Uploading");
        progressDialog.show();
        StorageReference reference = storageReference.child(STORAGE_PATH+ System.currentTimeMillis()+"."+ imageUri);
        reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                String NAME = Createname.getText().toString();
                String EMAIL = CreateEmail.getText().toString();
                Person person = new Person(NAME,EMAIL,taskSnapshot.getDownloadUrl().toString());
                String id = databaseReference.push().getKey();
                databaseReference.child(id).setValue(person);
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(),"Data Uploaded",Toast.LENGTH_LONG).show();

            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        progressDialog.dismiss();
                        Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();

                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {

                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double totalProgress= (100*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
                        progressDialog.setMessage("Uploaded %"+(int)totalProgress);

                    }
                });


    }else {
        Toast.makeText(getApplicationContext(),"Please Select Data First",Toast.LENGTH_LONG).show();
    }
}
public void viewAllData(View view){

}

} The Images that need to upload look like this, whitish touch

2 个答案:

答案 0 :(得分:1)

也许你错过了下面的事情:

  1. 授予访问设备存储空间的权限,了解如何通过https://developer.android.com/training/permissions/requesting.html

  2. 申请权限
  3. 使用Firebase存储而不是将本地照片URI更新为用户的更新请求:

  4. 请记住,您需要将文件上传到Firebase存储。成功上传后,您将获得一个URI。在UserUpdateRequest实例中将该URI用作用户的配置文件照片URI。这正是来自服务器的URI。否则,URI只是来自用户设备的本地文件,当用户更换设备时,它不会更新。

答案 1 :(得分:-1)

您需要在Android清单中授予权限才能上传图片。

The Permissions are: