2016-03-05 110 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

我已经更新了答案,包括捕捉图像中的一些细节 –

相关问题