package com.pappu5.navigation;
public class AddPost extends AppCompatActivity {
private Toolbar toolbar;
private ImageView postImage;
private Button postButton;
private EditText postDesc;
private static final int gallery_no = 1;
private ProgressDialog pd;
private FirebaseUser user;
private StorageReference sr;
private DatabaseReference dr;
private Uri postImageUri = null;
private Bitmap compressor;
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
protected String getSaltString() {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String saltStr = salt.toString();
return saltStr;
}
String uni = getSaltString();
public String getUni(){
return this.uni;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_post);
toolbar = findViewById(R.id.postBar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Add New Post !");
postImage = findViewById(R.id.postImage);
postDesc = findViewById(R.id.inputDesc);
postButton = findViewById(R.id.post);
sr = FirebaseStorage.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
String id = user.getUid();
dr = FirebaseDatabase.getInstance().getReference().child("Posts").child(uni);
dr.keepSynced(true);
postImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gallery = new Intent();
gallery.setType("image/*");
gallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery,"Select Image"),gallery_no);
}
});
postButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String desc = postDesc.getText().toString();
if(!TextUtils.isEmpty(desc) && postImageUri!=null ){
pd = new ProgressDialog(AddPost.this);
pd.setTitle("Uploading..");
pd.setMessage("Please Wait ..");
pd.setCanceledOnTouchOutside(false);
pd.show();
final String id = user.getUid();
File thumb = new File(postImageUri.getPath());
try {
compressor = new Compressor(AddPost.this)
.setMaxWidth(200)
.setMaxHeight(200)
.setQuality(75)
.compressToBitmap(thumb);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressor.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] thumb_byte = baos.toByteArray();
final StorageReference path = sr.child("Post_Images").child(id + ".jpg");
final StorageReference thumb_nail = sr.child("Post_Images").child("Thumb_Nails").child(id+".jpg");
path.putFile(postImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
path.putFile(postImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
path.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final String down_url = uri.toString();
UploadTask uploadTask = thumb_nail.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
thumb_nail.putFile(postImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
thumb_nail.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final String thumb_down_url = uri.toString();
Map <String,Object> update_map = new HashMap<>();
update_map.put("image", down_url);
update_map.put("thumb_image", thumb_down_url);
update_map.put("desc",desc);
update_map.put("timestamp", date);
dr.updateChildren(update_map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(AddPost.this, "Uploaded !", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AddPost.this,MainActivity.class);
startActivity(intent);
finish();
pd.dismiss();
} else {
Toast.makeText(AddPost.this, "Error in uploading thumbnail!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
});
}
});
}
});
}
});
}
});
}
});
} else {
Toast.makeText(AddPost.this, "Error!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
});
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == gallery_no && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
CropImage.activity(imageUri).setAspectRatio(1, 1).setMinCropWindowSize(500, 500)
.start(AddPost.this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
postImageUri = result.getUri();
postImage.setImageURI(postImageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
}
我只能输出用户数。如何获取数组中每个级别的用户ID?
答案 0 :(得分:0)
我认为问题在于,您必须将$ index变量设置为for
答案 1 :(得分:0)
我前段时间做了类似的事情:
public static function getChildren($idParent) {
$elem = [];
$childrens = Arbol::model()->findAll("estado = 1 and id_parent = " . $idParent . ' ORDER BY level');
foreach ($childrens as $key => $child) {
$persona = Persona::model()->findbyPk($child->id_child);
if ((int) $persona->id_superior === $idParent) {
$nombre = explode(" ", $persona->nombres);
$elem[$key] = [
"id" => $persona->id_persona,
"image" => Yii::app()->baseUrl . '/images/user-icon' . ($persona->pago_inscripcion ? '-activo.png' : '.png'),
"text" => [
'title' => $nombre[0] . ' ' . substr($persona->apellido_paterno, 0, 1) . '.'
]
];
} else {
$keySUperior = array_search($persona->id_superior, array_column($elem, "id"));
if (is_int($keySUperior)) {
$elem[$keySUperior]["children"] = self::getChildren((int) $persona->id_superior);
}
}
}
return $elem;
}
希望我能给您任何帮助