在Android中的Recyclerview上显示来自firebase的解码图像

时间:2016-09-12 15:21:06

标签: android firebase firebase-realtime-database

我有一个片段,用户用相机拍照,然后上传到Firebase。我将图像编码为一个没有问题的字符串。我在Firebase控制台中看到转换后的图像字符串。我遇到的问题是从Firebase解码图像字符串以显示为Recyclerview中的图像时没有显示任何内容。

如果您可以帮助我了解我的问题以及如何纠正它,我会非常感激。我一直试图通过阅读其他Stackexchange帖子来解决这个问题3天,但我无法弄清楚我的问题在哪里。

感谢。

我的片段:

public class AddImageFragment extends Fragment {

public static final int REQUEST_IMAGE_CAPTURE = 111;

private EditText progressDetail;

private Button cameraButton;
private Button saveButton;
private Button cancelButton;

private ImageView capturedImage;

private DatabaseReference mDatabaseReference;

public ImageProgress mImageProgress;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.add_image_fragment, container, false);


    progressDetail = (TextInputEditText) v.findViewById(R.id.progressStatusEditText);
    capturedImage = (ImageView) v.findViewById(R.id.capturedImageView);

    cameraButton = (Button) v.findViewById(R.id.takePictureButton);
    saveButton = (Button) v.findViewById(R.id.saveButton);

    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openCamera();
        }
    });


    //initialize firebase database
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();


    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //get current user
            FirebaseUser user =FirebaseAuth.getInstance().getCurrentUser();
            String uid = user.getUid();

            try {
                Bitmap image = decodeFromFirebaseBase64(mImageProgress.getImageUrl());
                capturedImage.setImageBitmap(image);
            }catch (IOException e){
                e.printStackTrace();
            }

            myNewImage(uid, progressDetail.getText().toString(),capturedImage.toString());




            getActivity().onBackPressed();

        }
    });
    return v;

}


private void myNewImage(String uid, String progressDetails, String imageUrl) {
    ImageProgress imageProgress = new ImageProgress(progressDetails, imageUrl);
    mDatabaseReference.child("Progress").child(uid).child("Images").push().setValue(imageProgress);

}


private void openCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        capturedImage.setImageBitmap(imageBitmap);
        encodeBitmapAndSaveToFirebase(imageBitmap);
    }

}


public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
    //current user
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String uid = user.getUid();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    String imageEncoded = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    reference.child("Progress").child(uid).child("imageUrl").push().setValue(imageEncoded);
}

public static Bitmap decodeFromFirebaseBase64(String image) throws IOException {

    byte[] decodeByteArray = android.util.Base64.decode(image, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodeByteArray, 0, decodeByteArray.length);

}

}

活动

 public class WeightProgressActivity extends BaseActivity {


public ImageProgress mImageProgress;
private FloatingActionButton fab;

ScaleAnimation shrinkAnim;

private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
public ImageView capturedImage;


//firebase database reference
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();

//get current user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.weight_progress);
    setNavDrawer(new MainNavDrawer(this));



    //initialize recyclerview
    mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);

    shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    if(mRecyclerView != null){
        mRecyclerView.setHasFixedSize(true);
    }

    //using linearlayout manager in recyclerview
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);


    //firebaserecycleradapter (firebase ui)
    FirebaseRecyclerAdapter<ImageProgress, ImageViewHolder> adapter = new FirebaseRecyclerAdapter<ImageProgress, ImageViewHolder>(
            ImageProgress.class, R.layout.weight_progress_list, ImageViewHolder.class, mDatabaseReference.child("images").child(uid).
            child("Progress Images").getRef()
    ) {
        @Override
        protected void populateViewHolder(ImageViewHolder viewHolder, ImageProgress model, int position) {
            viewHolder.progressDetail.setText(model.getProgressDetail());
            try {
                Bitmap image = decodeFromFirebaseBase64(mImageProgress.getImageUrl());
                viewHolder.capturedImage.setImageBitmap(image);
            }catch (IOException e){
                e.printStackTrace();
            }


        }
    };

    mRecyclerView.setAdapter(adapter);

    fab = (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,
                    new AddImageFragment()).addToBackStack(null).commit();

            //animation to make actionbar disappear
            shrinkAnim.setDuration(400);
            fab.setAnimation(shrinkAnim);
            shrinkAnim.start();
            shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //changing floating actionbar visibility to gone on animation end
                    fab.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });

        }
    });

}

//on back press code
@Override
public void onBackPressed() {
    super.onBackPressed();
    if (fab.getVisibility() == View.GONE)
        fab.setVisibility(View.VISIBLE);
}

//weightViewHolder method
public static class ImageViewHolder extends RecyclerView.ViewHolder{

    EditText progressDetail;
    ImageView capturedImage;




    public ImageViewHolder(View v){
        super(v);
        progressDetail = (EditText) v.findViewById(R.id.progressStatusEditText);
        capturedImage = (ImageView) v.findViewById(R.id.capturedImageView);





    }




}
public static Bitmap decodeFromFirebaseBase64(String image) throws IOException {

    byte[] decodeByteArray = android.util.Base64.decode(image, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodeByteArray, 0, decodeByteArray.length);

}

}

0 个答案:

没有答案