Link Image/gif Uploaded By Non-authenticated User To The Text Post By Same User
I have seen all related posts on stackoverflow, this is not a duplicate. Please read the whole query once. I'm making an app that allows users to write a post which may or may not contain an image/gif. 'The app has no form of authentication whatsoever'. Anybody can just open the app and add a post to database. The app is android and firebase-based. I have two questions. Even if the questions are stupid, please just help me.
- How to allow uploading gif in imageview without glide?
- If user writes something in textview and uploads an image from mobile in imageview in the same post, then how do I link them together in one post? (User should be able to post without image too) I'm struggling with this part because there's no user id to link uploaded image in database.
Please see my code and try to base your answer on that. I'll be really grateful.
I tried glide, I don't want to use it in my app. I want to do this without adding heavy libraries, if possible.
I know I have to get a downloadurl and link it in database, but every example I saw, had it based on user id, but my user is not authenticated. It could just be a robot writing to my database and even the robot should be allowed to. So just please guide me a bit.
public class PostActivity extends AppCompatActivity {
public static int RESULT_LOAD_IMAGE = 1;
private ImageButton newPostImageBtn;
private ImageView newPostImage;
private EditText newPostDesc;
private Button newPostBtn;
private View v;
List<Post> posts;
DatabaseReference databasePosts;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.HomeTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
databasePosts = FirebaseDatabase.getInstance().getReference("posts");
posts = new ArrayList<>();
newPostImageBtn = findViewById(R.id.imageBtn);
newPostImage = findViewById(R.id.imageView);
newPostDesc = findViewById(R.id.textDesc);
newPostBtn = findViewById(R.id.postBtn);
newPostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//calling the method addArtist()
//the method is defined below
//this method is actually performing the write operation
addPost();
}
});
newPostImageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getImageFromAlbum();
}
});
}
private void addPost() {
//getting the values to save
String name = newPostDesc.getText().toString().trim();
//checking if the value is provided
if (!TextUtils.isEmpty(name)) {
//getting a unique id using push().getKey() method
//it will create a unique id and we will use it as the Primary Key for our Artist
String id = databasePosts.push().getKey();
//creating an Artist Object
Post post = new Post(id, desc);
//Saving the Artist
databasePosts.child(id).setValue(post);
//setting edittext to blank again
newPostDesc.setText("");
//displaying a success toast
Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
} else {
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
}
}
private void getImageFromAlbum() {
try {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
} catch (Exception exp) {
Log.i("Error", exp.toString());
}
}
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
newPostImage.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
}
I want anonymous user to just open my app, click the post button, go to a page where there's empty textview and empty imageview and user can click on a button, upload image from mobile or not upload an image, and then just post when post button is clicked regardless of if image is there in imageview.
Answer
Create a UUID (or a unique identifier)
Option 1:
UUID id = UUID.randomUUID();
//printing id will print something like: f4708c4a-185c-4200-8033-cad28ffa2801
Option 2:
// This will create an "ID" of 10 char.
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder(10);
for (int i = 0; i < 10; i++) {
int index = (int) (alphabet.length() * Math.random());
sb.append(alphabet.charAt(index));
}
//printing sb.toString() will print something like: q3x3LcAK39
Use a Model
public class PostModel {
String id;
Bitmap image; //whatever your image is
String text;
public PostModel(String id, Bitmap image, String text) {
this.id = id;
this.image = image;
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Putting it all together
When I click on a button to submit the post, I want to create a new PostModel.
String id = sb.toString(); //option 2 from above
Bitmap image = image.getImageFromImageView(); //or whatever
String text = textView.getText().toString().trim();
PostModel post = new PostModel(id, image, text);
This will create a Post for us, we can then call Firebase to send it to your server... I wont do that.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM