我是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 –
是的,這是我猜。無論如何你都應該在後臺線程中進行像圖像處理這樣的繁重操作。這樣你就沒有問題了。 –