我是android新手。我试图捕捉图像并将其存储在firebase中。由于我们需要将图像转换为字符串,然后将其存储在firebase中,因此我正在使用base64算法。使用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.
。
如果我评论图像处理线,它工作得很好。 有人可以告诉我如何避免这样的错误,以便图像立即上传到我的数据库。
如果图像尺寸较大,则执行该在后台线程中操作。使用'AsyncTask'。 –
图像文件大小是1.01 MB –
是的,这是我猜。无论如何你都应该在后台线程中进行像图像处理这样的繁重操作。这样你就没有问题了。 –