【问题标题】:Image upload and download图片上传和下载
【发布时间】:2016-10-09 04:30:25
【问题描述】:

我们正在尝试使用以下代码 sn-p 上传图片:

       protected void onActivityResult(int requestCode, int resultcode, Intent intent) 
    {
            super.onActivityResult(requestCode, resultcode, intent);
      if (requestCode == 1)
     {
        if (intent != null && resultcode == RESULT_OK)
        {             

              Uri selectedImage = intent.getData();

              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);           
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String filePath = cursor.getString(columnIndex);
              cursor.close();
              if(bmp != null && !bmp.isRecycled())
              {
                  bmp = null;               
              }

              bmp = BitmapFactory.decodeFile(filePath);
              iv.setBackgroundResource(0);
             iv.setImageBitmap(bmp);    

             File file = new File(filePath);
             try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             try
             {
             Class.forName("com.mysql.jdbc.Driver");
             con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root", "virus");
             PreparedStatement ps = 
                       con.prepareStatement("Update user_info set image=? WHERE ID=\""+id+"\"");

                ps.setBinaryStream(1,fis,(int)file.length());

                    ps.executeUpdate();
                    ps.close();
                    fis.close();
             }
             catch(Exception e)
             {
                 Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
             }

        }
        else
        {
            Log.d("Status:", "Photopicker canceled");           
        }

        }

在 mysql DB 中上传后,我们得到 [BLOB - 60.1KiB].. 图片是否上传成功? 我们正在尝试通过以下代码检索它

 try {
     Class.forName("com.mysql.jdbc.Driver");
     con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root","virus");
     Statement st = con.createStatement();
     String query = "Select image from user_info where ID=\""+id+"\"";


     PreparedStatement pstmt = con.prepareStatement(query);
     ResultSet rset = pstmt.executeQuery();
        rset.next();
        byte[] blob = rset.getBytes(1);//getBlob(1); 
     if(blob != null) {       
         byte[] decodedString = Base64.encode(blob, Base64.DEFAULT);      
         Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);   
         iv.setImageBitmap(decodedByte);      
         }  else {
             iv.setImageResource(R.drawable.chit_chat);         }

但我们无法在图像视图中显示。

【问题讨论】:

    标签: android android-emulator


    【解决方案1】:

    直接从 android 使用 sql 连接不是一个好主意,最好使用 Web 服务或负责将内容放入数据库和从数据库返回的套接字连接。

    数据库连接成本高,需要比 httpconnections 更稳定。不适合移动环境。 Here 是一个很好的话题。

    此外,在您的服务器附近拥有一个数据库将帮助您分离关注点。您将能够在服务器上测试从 DB 获得的字节是否符合您的预期,并使用简单的异步任务在 android 上获取图像。

    如果你想坚持你的解决方案,你应该打印出从数据库接收到的字节,你为什么使用 base64 ?

    【讨论】:

      【解决方案2】:

      使用解码代替编码

      byte[] decodedString = Base64.decode(blob, Base64.DEFAULT);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-16
        • 1970-01-01
        • 2017-11-07
        • 2012-09-10
        • 1970-01-01
        相关资源
        最近更新 更多