图像在Android应用程序中保存为文本

时间:2016-05-02 12:01:39

标签: php

您好我正在尝试从我的应用程序上传图像并希望将其保存在我的网络文件夹中我已经使用了下面提到的PHP代码但是文件被保存为txt,但是我需要jpeg文件用于我的未来使用

PHP代码 -

<?php  
// Get image string posted from Android App  
 $base=$_REQUEST['image'];  
  // Get file name posted from Android App  
  $filename = $_REQUEST['name'];  
  // Decode Image  
 $binary=base64_decode($base);  
 header('Content-Type: bitmap; charset=utf-8');  
  // Images will be saved under 'www/domain.com/images' folder  
 $file = fopen('images/'.$filename, 'wb');  
  // Create File  
  fwrite($file, $binary,);  
 fclose($file);  
 echo 'Image upload complete, Please check your php file directory';  
 ?>  

3 个答案:

答案 0 :(得分:1)

在您从应用中发布图片时试用此代码

try {
            Uri myUri = Uri.parse(image1);
            Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(myUri));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            image = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


       List<NameValuePair> params1 = new ArrayList<NameValuePair>();

        params1.add(new BasicNameValuePair("image", image));

答案 1 :(得分:0)

此处,以下代码可帮助您使用PHP将图像存储在服务器上。我假设您在post参数中获取图像名称。

$binary = base64_decode($image);
$filename = $_REQUEST['name'];  
file_put_contents('images/'.$filename, $binary);

答案 2 :(得分:0)

我已经从我这边做过了,但感谢你们的支持。

  <?php  
// Get image string posted from Android App  
$base=$_REQUEST['image'];  
// Get file name posted from Android App  
$filename = $_REQUEST['name'].'.jpg';  
// Decode Image  
$binary=base64_decode($base);  
header('Content-Type: bitmap; charset=utf-8');  
// Images will be saved under 'www/domain.com/images' folder  
$file = fopen('images/'.$filename, 'wb');  
 // Create File  
 fwrite($file, $binary,);  
fclose($file);  
echo 'Image upload complete, Please check your php file directory';  
?>