Android拍照上传至PHP服务器并写入MySql数据库(下)

2 篇文章 0 订阅
2 篇文章 0 订阅

Android实现

调用系统相机,拍照:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getFileUri();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
            startActivityForResult(intent, CODE_CAMERA);


  private void getFileUri() {
    image_name = Calendar.getInstance().getTimeInMillis() + ".jpg";
    file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + image_name);
    file_uri = Uri.fromFile(file);
}

在onActivityResult里面接收图片并Base64处理:

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CODE_CAMERA && resultCode == RESULT_OK) {
        new EncodeImage().execute();   //把bitmap转换成base64字符串
    }
}

EncodeImage是一个AsyncTask,doInBackground里面从uri里面获取bitmap,然后转入输出流,最终转换为base64编码字符串:

  @Override
    protected Void doInBackground(Void... voids) {
        bitmap = BitmapFactory.decodeFile(file_uri.getPath());
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        byte[] array = stream.toByteArray();
        encoded_string = Base64.encodeToString(array, 0);
        bitmap.recycle();  //防止oom
        return null;
    }

然后就可以上传到服务器了:

    private void uploadImage() {
    HashMap<String, String> map = new HashMap<>();
    map.put("encoding_string", encoded_string);
    map.put("image_name", image_name);
    OkHttpUtils.post()
            .url("http:192.168.0.112/phpdemo/uploadimage.php")
            .params(map)
            .tag(this)
            .build()
            .execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.e("出错了", "错误信息:" + e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.e("成功or失败", "信息:" + response);
                }
            });
}

在上传服务器过程中,遇到两个问题,第一,提示POST Content-Length of ... bytes exceeds the limit of 8388608 bytes,这个错误是因为php默认最大post上传8M,更改php.ini里面的post_max_size=1000M就ok了;第二,当第二次拍照的时候会出现OOM的情况,检查代码发现bitmap没有recycle。

OVER

您好!您可以使用Python的os和BeautifulSoup库来爬取本地多个文件夹下的HTML数据,并使用Python的MySQL Connector库将数据写入MySQL数据库首先,需要安装以下库: BeautifulSoup:用于解析HTML数据 - MySQL Connector:用于连接MySQL数据库 您可以使用以下代码来实现这个功能: ```python import os from bs4 import BeautifulSoup import mysql.connector # 配置MySQL数据库连接 config = { 'user': 'your_username', 'password': 'your_password', 'host': 'localhost', 'database': 'your_database_name', 'raise_on_warnings': True } # 连接MySQL数据库 conn = mysql.connector.connect(**config) cursor = conn.cursor() # 遍历文件夹 root_folder = 'your_root_folder_path' for root, dirs, files in os.walk(root_folder): for file in files: if file.endswith('.html'): file_path = os.path.join(root, file) # 读取HTML文件内容 with open(file_path, 'r') as f: html_content = f.read() # 解析HTML数据 soup = BeautifulSoup(html_content, 'html.parser') # 提取需要的数据 # ... # 将数据写入MySQL数据库表 # ... # 提交事务 conn.commit() # 关闭数据库连接 cursor.close() conn.close() ``` 在上述代码中,您需要根据实际情况进行相应的配置,如MySQL数据库的用户名、密码、主机名、数据库名,以及根文件夹的路径。然后,遍历文件夹中的HTML文件,读取文件内容并使用BeautifulSoup解析HTML数据,然后将提取的数据写入MySQL数据库中。 请注意,上述代码仅提供了一个框架,您需要根据实际需要进行适当的修改和完善,包括提取需要的数据和将数据写入数据库的具体实现。 希望能对您有所帮助!如有任何疑问,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值