[英]Android - How to store an image inside the Firestore folder which is already created
public void upload() {
if(imageUri!=null) {
StorageReference reference=storageReference.child("customers/");
reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Toast.makeText(getApplicationContext(),"File Uploaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double totalProgress=(100*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
// Toast.makeText(getApplicationContext(),"File uploaded"+(int)totalProgress,Toast.LENGTH_LONG).show();
}
});
}
}
In Firebase storage, there is one folder called "customers" where I want to store the selected image. 在Firebase存储中,有一个名为“客户”的文件夹,我想在其中存储所选图像。 When I try to upload the image, it is storing it as a different folder, not the folder which is already created. 当我尝试上传的图片,它是把它作为一个不同的文件夹,而不是它已经创建的文件夹。 Here, "customers" is the already created folder. 在这里,“客户”是已经创建的文件夹。
Try to get file from this file, it could be because of incorrect reference to your folder. 尝试从此文件中获取文件,这可能是因为对您的文件夹的引用不正确。 Also show code where is your getReference()
method is called or it is better how you set reference to your FirebaseStorage. 还显示代码,在何处调用getReference()
方法,或者更好地设置对FirebaseStorage的引用。
public void upload(String imageName) {
if(imageUri!=null) {
StorageReference reference=storageReference.child("customers).child(imageName);
reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Toast.makeText(getApplicationContext(),"File Uploaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double totalProgress=(100*taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount();
// Toast.makeText(getApplicationContext(),"File uploaded"+(int)totalProgress,Toast.LENGTH_LONG).show();
}
});
}
}
In order to retrieve the image name use:- 为了检索图像名称,请使用:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData( );
String picturePath = getPath( getActivity( ).getApplicationContext( ), selectedImageUri );
Log.d("Picture Path", picturePath);
upload(picturePath);
}
}
public static String getPath( Context context, Uri uri ) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.