英文:
FireBase Storage getDownloadUrl() .Getting error in continueWithTask method
问题
#错误: 非静态方法<TContinuationResult>continueWithTask(Continuation<TResult,Task<TContinuationResult>>)不能在静态上下文中引用,其中TContinuationResult,TResult是类型变量:#
**这是错误出现的代码部分,与continueWithTask有关**<br>
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
<details>
<summary>英文:</summary>
#Error: non-static method <TContinuationResult>continueWithTask(Continuation<TResult,Task<TContinuationResult>>) cannot be referenced from a static context where TContinuationResult,TResult are type-variables:#
** This the code where error is with continueWithTask**<br>
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();
}
}
});
}
});
}
#my whole Code #
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);
}
}
}
</details>
# 答案1
**得分**: 0
`continueWithTask`是一个实例方法,而不是静态方法,因此您需要使用实例变量`uploadTask`来调用`continueWithTask`:
```java
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, "成功获取产品图片,保存到数据库...", Toast.LENGTH_SHORT).show();
SaveProductInfoToDatabase();
}
}
});
英文:
continueWithTask
is an instance method not a static method, therefore you need to use the instance variable uploadTask
to call 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();
}
}
});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论