在我的项目中,我从自定义视图中创建一个位图,这是一个绘图。我不能一次上传很多图纸,因为它们会被覆盖。之后,我获取Bitmap的URI并将绘图上传到FirebaseStorage。我收到一条错误消息:无效请求。缺少X-Goog-Upload-Command标头。此外,当我从FirebaseStorage转到下载URL时,存储了图形,我收到此错误:
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}
这是我的代码:
public class SharedClass extends AppCompatActivity {
StorageReference firebaseStorage;
DatabaseReference databaseReference;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
String uID;
String imgFileName;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
}
public void uploadImageToStorage(final Context context, Uri imageURI, String chosenTopic, String typeOfImage) {
// Toast.makeText(context, "Context: " + context, Toast.LENGTH_LONG).show();
// Toast.makeText(context, "Image URI: " + imageURI, Toast.LENGTH_LONG).show();
// Toast.makeText(context, "chosen topic : " + chosenTopic, Toast.LENGTH_LONG).show();
// Toast.makeText(context, "Type of image: " + typeOfImage, Toast.LENGTH_LONG).show();
firebaseAuth = firebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
uID = currentUser.getUid().toString();
//Toast.makeText(context, "uID: " + uID, Toast.LENGTH_LONG).show();
databaseReference = FirebaseDatabase.getInstance().getReference()
.child(uID)
.child("sak_1")
.child(chosenTopic)
.child("images");
firebaseStorage = FirebaseStorage.getInstance().getReference();
StorageReference filepath = firebaseStorage.child(uID).child(typeOfImage + String.valueOf(imageURI.getLastPathSegment()));
filepath.putFile(imageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(context, "Laddade upp bild", Toast.LENGTH_LONG).show();
Uri imageDownloadURL = taskSnapshot.getUploadSessionUri();
//Call uploadImageToDatabase with taskSnapshot.getDowloadURI
uploadImageToDatabase(imageDownloadURL);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, "Bilden laddades inte upp", Toast.LENGTH_LONG).show();
Log.i("AppInfo", String.valueOf(e.getCause()));
}
});
}
public class DrawingFragment extends Fragment {
Button newDrawingButton;
StorageReference drawingsRef;
FirebaseAuth firebaseAuth;
FirebaseUser currentUser;
String uID;
public DrawingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_drawing, container, false);
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
uID = currentUser.getUid().toString();
drawingsRef = FirebaseStorage.getInstance().getReference()
.child(uID);
newDrawingButton = (Button) v.findViewById(R.id.new_drawing_button);
newDrawingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newDrawing();
}
});
return v;
}
public void newDrawing() {
final AlertDialog.Builder drawingDialogBuilder = new AlertDialog.Builder(getActivity());
final CustomDrawer customDrawer = new CustomDrawer(getContext(), null, 5f);
customDrawer.setDrawingCacheEnabled(true);
customDrawer.buildDrawingCache();
customDrawer.setBackgroundColor(Color.WHITE);
drawingDialogBuilder.setView(customDrawer);
drawingDialogBuilder.setNeutralButton("Spara", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doneWithDrawing(customDrawer);
dialog.cancel();
}
});
AlertDialog drawDialog = drawingDialogBuilder.create();
drawDialog.getWindow().setLayout(500, 500);
drawDialog.show();
}
public void doneWithDrawing(View drawedView) {
Bitmap drawedImageBitmap = drawedView.getDrawingCache();
//The byte array that the stream is written into.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//compress the bitmap into the stream.
drawedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//data contains the bytes of the compressed bitmap
byte[] data = baos.toByteArray();
//Upload the task so that we can get the download uri.
UploadTask uploadTask = drawingsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri drawedImageUri = taskSnapshot.getUploadSessionUri();
uploadDrawing(drawedImageUri);
}
});
}
public void uploadDrawing(Uri uri) {
String typeOfImage = String.valueOf("dawing");
SharedClass sharedClass = new SharedClass();
sharedClass.uploadImageToStorage(getActivity(), uri, getArguments().getString("chosenTopic"), typeOfImage);
}
}
public class CustomDrawer extends View {
Paint paint;
Path path;
public CustomDrawer(Context context, AttributeSet attributeSet, float paintWidth) {
super(context, attributeSet);
paint = new Paint();
path = new Path();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(paintWidth);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float xPos = event.getX();
float yPos = event.getY();
//What kind of event were there?
switch (event.getAction()) {
//Started to draw
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
break;
//Moved the finger
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
break;
//Released finger
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
}
存储中的图纸