【发布时间】:2016-03-05 06:18:55
【问题描述】:
我是安卓新手。我正在尝试捕获图像并将其存储在 firebase 中。由于我们需要将图片转成字符串然后存储到firebase中,所以我使用的是base64算法。
获取图像并将其存储在数据库中的方法是:
public void capturePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(photo);
}
}
public void storeDatabase(View v)
{
EditText editRollno = (EditText) findViewById(R.id.rollno);
EditText editname = (EditText) findViewById(R.id.name);
EditText editmarks = (EditText) findViewById(R.id.marks);
Firebase ref1 = ref.child("student_information").child("Student" + n);
ref1.child("Name").setValue(editname.getText().toString());
ref1.child("Rollno").setValue(editRollno.getText().toString());
ref1.child("Marks").setValue(editmarks.getText().toString());
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.abc);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
ref1.child("Image").setValue(imageFile);
n++;
}
当我点击提交按钮时,上传值需要很长时间。而且,很多时候我可以看到这条线
Skipped 537 frames! The application may be doing too much work on its main thread.
在 Android 监视器中。
如果我对图像处理行发表评论,则一切正常。 有人可以告诉我如何避免此类错误,以便图像立即上传到我的数据库中。
【问题讨论】:
-
如果图像很大,则在后台线程中执行此操作。使用
AsyncTask。 -
图片文件大小为1.01 MB
-
是的,我猜这很大。无论如何,您都应该在后台线程中执行繁重的操作,例如图像处理。这样你就没有问题了。
-
上传图片之前可以压缩图片
标签: android firebase firebase-realtime-database