作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
** 这是 continueWithTask 出现错误的代码**
private void StoreProductInformation()
{
loadingBar.setTitle("Add New Product");
loadingBar.setMessage("Dear Admin,Please wait....., while we are adding the new Product");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
Calendar calendar=Calendar.getInstance();
SimpleDateFormat CurrentDate=new SimpleDateFormat("MM DD,YYYY");
SaveCurrentDate=CurrentDate.format(calendar.getTime());
SimpleDateFormat CurrentTime=new SimpleDateFormat("HH:mm:ss a");
SaveCurrentTime=CurrentTime.format(calendar.getTime());
ProductRsndomKey=SaveCurrentDate + SaveCurrentTime;
final StorageReference filePath = ProdductImageRef.child(ImageUri.getLastPathSegment() + ProductRsndomKey + ".jpg");
final UploadTask uploadTask=filePath.putFile(ImageUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
String message=e.toString();
Toast.makeText(AdminAddNewProductActivity.this,"Error: "+message,Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
Toast.makeText(AdminAddNewProductActivity.this,"Image uploaded successfully... ",Toast.LENGTH_SHORT).show();
Task<Uri> uriTask = UploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
{
if(!task.isSuccessful())
{
throw task.getException();
}
downloadImageUrl=filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful())
{
downloadImageUrl=task.getResult().toString();
Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
SaveProductInfoToDatabase();
}
}
});
}
});
}
package com.example.ecommerce;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
public class AdminAddNewProductActivity extends AppCompatActivity {
private String CategoryName , Description,Price,Pname,SaveCurrentDate,SaveCurrentTime;
private ImageView InputProductImage;
private Button AddNewProductButton;
private EditText InputProductName,InputProductDescription,InputProductPrice;
private static final int GalleryPick=1;
private Uri ImageUri;
private String ProductRsndomKey , downloadImageUrl;
private StorageReference ProdductImageRef;
private DatabaseReference ProductsRef;
private ProgressDialog loadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_add_new_product);
CategoryName=getIntent().getExtras().get("category").toString();
ProdductImageRef = FirebaseStorage.getInstance().getReference().child("Product Images");
ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
AddNewProductButton=findViewById(R.id.add_new_product);
InputProductImage=findViewById(R.id.select_products_image);
InputProductName=findViewById(R.id.product_name);
InputProductDescription=findViewById(R.id.product_description);
InputProductPrice=findViewById(R.id.product_price);
loadingBar=new ProgressDialog(this);
InputProductImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenGallary();
}
});
AddNewProductButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
ValidateProductData();
}
});
}
private void ValidateProductData()
{
Description=InputProductDescription.getText().toString();
Price=InputProductPrice.getText().toString();
Pname=InputProductName.getText().toString();
if(ImageUri == null)
{
Toast.makeText(this,"Product Image is required...",Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(Description))
{
Toast.makeText(this,"Please write product description...",Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(Price))
{
Toast.makeText(this,"Please write product Price...",Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(Pname))
{
Toast.makeText(this,"Please write product Name...",Toast.LENGTH_SHORT).show();
}
else
{
StoreProductInformation();
}
}
private void StoreProductInformation()
{
loadingBar.setTitle("Add New Product");
loadingBar.setMessage("Dear Admin,Please wait....., while we are adding the new Product");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
Calendar calendar=Calendar.getInstance();
SimpleDateFormat CurrentDate=new SimpleDateFormat("MM DD,YYYY");
SaveCurrentDate=CurrentDate.format(calendar.getTime());
SimpleDateFormat CurrentTime=new SimpleDateFormat("HH:mm:ss a");
SaveCurrentTime=CurrentTime.format(calendar.getTime());
ProductRsndomKey=SaveCurrentDate + SaveCurrentTime;
final StorageReference filePath = ProdductImageRef.child(ImageUri.getLastPathSegment() + ProductRsndomKey + ".jpg");
final UploadTask uploadTask=filePath.putFile(ImageUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
String message=e.toString();
Toast.makeText(AdminAddNewProductActivity.this,"Error: "+message,Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
{
Toast.makeText(AdminAddNewProductActivity.this,"Image uploaded successfully... ",Toast.LENGTH_SHORT).show();
Task<Uri> uriTask = UploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
{
if(!task.isSuccessful())
{
throw task.getException();
}
downloadImageUrl=filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful())
{
downloadImageUrl=task.getResult().toString();
Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
SaveProductInfoToDatabase();
}
}
});
}
});
}
private void SaveProductInfoToDatabase()
{
HashMap<String,Object> productMap=new HashMap<>();
productMap.put("pid",ProductRsndomKey);
productMap.put("date",SaveCurrentDate);
productMap.put("time",SaveCurrentTime);
productMap.put("discription",Description);
productMap.put("image",downloadImageUrl);
productMap.put("category",CategoryName);
productMap.put("price",Price);
productMap.put("pname",Pname);
ProductsRef.child(ProductRsndomKey).updateChildren(productMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if ((task.isSuccessful()))
{
Intent intent=new Intent(AdminAddNewProductActivity.this,AdminCategoryActivity.class);
startActivity(intent);
loadingBar.dismiss();
Toast.makeText(AdminAddNewProductActivity.this,"Product is added Successfully...",Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.dismiss();
String messg=task.getException().toString();
Toast.makeText(AdminAddNewProductActivity.this,"Error :"+messg,Toast.LENGTH_SHORT).show();
}
}
});
}
private void OpenGallary()
{
Intent galleryIntent=new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GalleryPick);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GalleryPick && resultCode == RESULT_OK && data != null)
{
ImageUri=data.getData();
InputProductImage.setImageURI(ImageUri);
}
}
}
最佳答案
continueWithTask
是一个实例方法,不是静态方法,因此需要使用实例变量uploadTask
来调用continueWithTask
:
final UploadTask uploadTask=filePath.putFile(ImageUri);
Task<Uri> uriTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
{
if(!task.isSuccessful())
{
throw task.getException();
}
downloadImageUrl=filePath.getDownloadUrl().toString();
return filePath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful())
{
downloadImageUrl=task.getResult().toString();
Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
SaveProductInfoToDatabase();
}
}
});
关于java - FireBase Storage getDownloadUrl()。在 continueWithTask 方法中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61094305/
我在所有三种方法上都收到此警告,我尝试在 stackoverflow 中搜索解决方案,但无法弄清楚可用的解决方案如何应用于我的情况。尽管代码按预期工作正常,但代码块以黄色突出显示,并且似乎不断要求注意
错误:无法从 TContinuationResult、TResult 为类型变量的静态上下文中引用非静态方法 continueWithTask(Continuation>): ** 这是 contin
除了同步与异步之外,它们文档中的差异让我感到困惑。他们的例子 github page继续看起来仍然被同步调用。 continueWith() 向此任务添加一个同步延续,返回一个在延续运行完成后完成的新
这是关于 Task . task.continueWith() 和 task.continueWithTask() 有什么区别,你能分别举个例子吗? 最佳答案 continueWith 之间的主要区别
我是一名优秀的程序员,十分优秀!