【问题标题】:Uploading an image from Android to MySQL database将图像从 Android 上传到 MySQL 数据库
【发布时间】:2012-07-23 12:22:05
【问题描述】:

美好的一天!我正在尝试搜索将图像文件从 Android 上传到在线 MySQL 数据库的基本教程,但我找不到。

我现在正在制作一个活动,可以将用户的个人资料图片从 Android 上传到在线服务器。

我需要的是显示一个按钮,当点击它时,用户可以从文件中选择一个图像。有人可以指导我这样做吗?提前致谢!

【问题讨论】:

标签: php android mysql


【解决方案1】:

在客户端,您可以这样做。

HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;    

String pathToOurFile = "path of the image.jpeg";
String urlServer = "http://xxx.xxx.xxx.xxx/uploader.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

try {
        FileInputStream fileInStream = new FileInputStream(new File(pathToOurFile) );

        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();

       // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // Responses from the server (code and message)
        serverResponseCode = connection.getResponseCode();
        serverResponseMessage = connection.getResponseMessage();

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
        //Exception handling
    }

服务器端

<?php
    $target_path  = "./";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
         echo "Success";
    } else{
        echo "Error";
    }
?>

【讨论】:

  • 其实我从某个地方找到了这个代码,但没有它的链接所以发布了我在我的应用程序中使用的代码。希望它也可以解决你的问题..问候。
  • 服务器响应代码、服务器响应消息、文件输入流。我不知道这些变量有什么作用。我想试试你的代码,但这些变量给了我错误。
  • 全部声明为字符串变量
  • fileInputStream 应该声明为 FileInputStream fileInputStream ;
【解决方案2】:

使用以下代码从 ImageGallery 中获取图片:

Intent i = new Intent(Intent.ACTION_PICK,
               android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

它会启动ImageGallery,现在你可以选择一个图像,在onActivityResult你可以将图像解码成位图,如链接中所述:here

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.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();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

接下来,您需要将该位图上传到服务器。为此,您可以使用 Haresh 的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2012-06-29
    • 2016-05-26
    相关资源
    最近更新 更多