【问题标题】:Converting image to string using the Base64 algorithm使用 Base64 算法将图像转换为字符串
【发布时间】: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


【解决方案1】:

为了将它存储到数据库中,我肯定会使用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");

【讨论】:

  • 谢谢。。如果我需要上传的图片被相机点击了怎么办?那么,我应该写什么来代替 R.drawable.abc?
  • 我已经更新了答案,在捕获图像时包含了一些细节
猜你喜欢
  • 2018-09-07
  • 2014-07-21
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
相关资源
最近更新 更多