2016-03-05 104 views
0

我是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. 

如果我評論圖像處理線,它工作得很好。 有人可以告訴我如何避免這樣的錯誤,以便圖像立即上傳到我的數據庫。

+1

如果圖像尺寸較大,則執行該在後臺線程中操作。使用'AsyncTask'。 –

+1

圖像文件大小是1.01 MB –

+0

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

回答

0

爲了將它存儲到數據庫中,我肯定會使用AsyncTask這個。所以,你可以有一個單獨的類,它具有以下功能:

private class ImageDBManager extends AsyncTask< String, Integer, Void> { 
    protected Long doInBackground(String... items) { 
    int count = items.length; 
    String editRoll = items[0]; 
    String editName = items[1]; 
    String editMarks = items[2]; 
    Firebase ref1 = ref.child("student_information").child("Student" + n); 
    ref1.child("Name").setValue(editname.getText().toString()); 
    ref1.child("Rollno").setValue(editRoll); 
    ref1.child("Marks").setValue(editName); 
    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); 
} 

protected void onProgressUpdate(Integer... progress) { 
    setProgressPercent(progress[0]); 
} 

protected void onPostExecute(){ 

} 

}

然後調用它像這樣:

public void storeDatabase(View v) { 
    ImageDBManager manager = new ImageDBManager(); 
    EditText editRollno = (EditText) findViewById(R.id.rollno); 
    EditText editname = (EditText) findViewById(R.id.name); 
    EditText editmarks = (EditText) findViewById(R.id.marks); 
    manager.execute(new String[] { editRollno.getText(), editname.getText(), editmarks.getText() } 

} 

如果從相機拍攝圖像,最好的參考就是這裏 http://developer.android.com/training/camera/photobasics.html

這是開始的活動和實施onActivityForResult瓦特同樣的想法在這裏,你得到的結果額外的數據,這樣反而讓從R.drawable.abc位圖,你會得到像這樣的onActivityForResult

Bitmap imageBitmap = (Bitmap) extras.get("data"); 
+0

謝謝..如果圖像我需要上傳被相機點擊嗎?那麼,我應該寫什麼來代替R.drawable.abc? –

+0

每個http://developer.android.com/training/camera/photobasics.html –

+0

我已經更新了答案,包括捕捉圖像中的一些細節 –

相關問題