gpt4 book ai didi

android - 如何避免 java.lang.StackOverflowError : stack size 8MB , 将图像保存到 Firebase 存储并将其下载 uri 到 Firebase 数据库

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:41 24 4
gpt4 key购买 nike

我正在尝试通过使用 camera 或从 gallery 拍摄新照片来从用户那里获取照片。效果很好,我可以获取照片并将其显示在屏幕上。我想要做的是,将图像保存到 Firebase 存储和 Firebase 数据库 中的链接。

public void selectImage()
{
final String[] items = {"Camera", "Gallery","Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a photo");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0)
{
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
mUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
else if(which == 1)
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Foto seçin"), SELECT_FILE);
}
else if(which == 2)
{
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
try {
Bitmap thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), mUri);
ExifInterface ei = new ExifInterface(getRealPathFromURI(mUri));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(thumbnail, 90);
break;

case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(thumbnail, 180);
break;

case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(thumbnail, 270);
break;

case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = thumbnail;
}
profilePicture.setImageBitmap(rotatedBitmap);
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == SELECT_FILE) {
mUri = data.getData();
profilePicture.setImageURI(mUri);
setPhoto(mUri);
}
}
final StorageReference reference = FirebaseStorage.getInstance().getReference().child("profile_photos/" + mUser.getNumber() + ".jpg");
reference.putFile(mUri);
reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference().child("Users").child(mUser.getNumber()).child("Picture");
reference1.setValue(uri);
}
});
}

当我想将图像保存到 Firebase 时,出现了 java.lang.StackOverflowError: stack size 8MB 错误。即使图像保存在存储中,数据库 中也没有任何变化。如何避免此错误并将图片的下载 uri 保存到 Database 中?

enter image description here

最佳答案

我遇到了同样的问题。事实证明,您不能 setValue URI。

尝试将您的 URI 转换为字符串,这样 fatal error 就会消失。

 reference1.setValue(uri.toString());

关于android - 如何避免 java.lang.StackOverflowError : stack size 8MB , 将图像保存到 Firebase 存储并将其下载 uri 到 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50887810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com