Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

android - How Can I add My Captured Image to My Hashmap

I am still learning hashmaps and database structures so please forgive me if this is a basic question. I've tried to research but nothing seems to work. I have a method in the same activity where I capture an image which is pushed to Firebase Storage. The image successfully goes to Firebase Storage, but I cannot figure out how to add the image to my hashmap to go to the Firebase Database. My hashmap is below and currently works. Thank you for your help.

    String title = title_edit_text.getText().toString();
    String first_name = firstname.getText().toString();
    String last_name = lastname.getText().toString();

    if (TextUtils.isEmpty(title)) {
        title_edit_text.setError("Input is required!");
    } else {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date date = new Date();
        String creation_date= dateFormat.format(date);
       
        DateFormat date_format_for_time = new SimpleDateFormat("hh:mm:ss a");
        Date time = new Date();
        String creation_time= date_format_for_time.format(time);

        HashMap userMap = new HashMap();
        userMap.put("title", title);
        userMap.put("creation_date", creation_date); 
        userMap.put("creation_time", creation_time); 
        userMap.put("first_name", first_name);
        userMap.put("last_name", last_name);

        String upload = UsersReference2.push().getKey();
        UsersReference2.child(upload).setValue(userMap).addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    sendBackActivity();
                    Toast.makeText(Adding_Activity.this, "Successfully Save.", Toast.LENGTH_LONG).show();

                } else {
                    String message = task.getException().getMessage();
                    Toast.makeText(AddingActivity.this, "Error. Did Not Save: " + message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Gallery_Pick && resultCode == RESULT_OK && data != null) {
        image_uri = data.getData();

        CropImage.activity(image_uri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(3, 2)
                .start(this);
    }

    // when pressing the crop button//
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();

            StorageReference filePath = CoverPostReference.child(currentUserID + ".jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if (task.isSuccessful()) {

                        Toast.makeText(Adding_Activity.this, "Image has been added sucessfully...", Toast.LENGTH_SHORT).show();
                        Task<Uri> result = task.getResult().getMetadata().getReference().getDownloadUrl();

                        result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                final String downloadUrl = uri.toString();
                                UsersReference2.child("cover_image").setValue(downloadUrl)
                                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {
                                                    Toast.makeText(Adding_Activity.this, "Image has been stored...", Toast.LENGTH_SHORT).show();
                                                } else {
                                                    String message = task.getException().getMessage();
                                                    Toast.makeText(Adding_Activity.this, "Error: " + message, Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        });
                            }
                        });
                    }
                }
            });
        } else {
            Toast.makeText(Adding_Activity.this, "Error: Image did not upload. Please try again.", Toast.LENGTH_SHORT).show();
        }
    }
}

1

question from:https://stackoverflow.com/questions/65648067/how-can-i-add-my-captured-image-to-my-hashmap

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You shouldn't/wouldn't store the image bitmap data into the hashmap, instead you should store the location to the bitmap (URI) to the hashmap (and database) and then use that to access the image.

Storing

Map<String, Uri> bitmapUriMap = new HashMap<String,Uri>();
bitmapUriMap.put("MyLocalBitmapName", UriOfBitmap);

Accessing

Then when you have the URI you just decode it

Uri uri = bitmapUriMap.get("MyLocalBitmapName);
Bitmap decodedUri = BitmapFactory.decodeFile(new File(uri.getPath()));

This is what you would store in the database. For instance if you want the image of a user, then you would store the image uri within your firebase database and then you would access it this way. Storing a bitmap in a database is not ideal and not recommended.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...