即時通訊使用PHP腳本,允許從圖庫中選擇一個圖像,並將其上傳到在線網絡託管服務。圖片被上傳,但上傳的.jpg文件是空的,被命名爲0.jpg,大小也被顯示爲0字節。當我嘗試打開圖像它顯示Php上傳圖像到服務器在Android
這可能是造成這種情況的可能原因「的形象http://testimage.site88.net/pic/0.jpg不能因爲它包含錯誤顯示 」? PHP代碼是如下
<?php
$name = $_POST['name']; //image name
$image = $_POST['image']; //image in string format
//decode the image
$decodedImage = base64_decode($image);
//upload the image
file_put_contents("pic/".$name.".jpg", $decodedImage);
的機器人的代碼是:預先
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity {
//define global views variable
public ImageView imageView;
public Button selectImage,
uploadImage;
public String SERVER = "http://testimage.site88.net/saveImage.php",
timestamp;
private static final String TAG = MainActivity.class.getSimpleName();
private static final int RESULT_SELECT_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//instantiate view
imageView = (ImageView) findViewById(R.id.imageView);
selectImage = (Button) findViewById(R.id.selectImage);
uploadImage = (Button) findViewById(R.id.uploadImage);
//when selectImage button is pressed
selectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//call the function to select image from album
selectImage();
}
});
//when uploadImage button is pressed
uploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get image in bitmap format
Bitmap image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//execute the async task and upload the image to server
new Upload(image,"IMG_"+timestamp).execute();
}
});
}
//function to select a image
private void selectImage(){
//open album to select image
Intent gallaryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallaryIntent, RESULT_SELECT_IMAGE);
}
/*
* This function is called when we pick some image from the album
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_SELECT_IMAGE && resultCode == RESULT_OK && data != null){
//set the selected image to image variable
Uri image = data.getData();
imageView.setImageURI(image);
//get the current timeStamp and strore that in the time Variable
Long tsLong = System.currentTimeMillis()/1000;
timestamp = tsLong.toString();
Toast.makeText(getApplicationContext(),timestamp,Toast.LENGTH_SHORT).show();
}
}
private String hashMapToUrl(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
//async task to upload image
private class Upload extends AsyncTask<Void,Void,String>{
private Bitmap image;
private String name;
public Upload(Bitmap image,String name){
this.image = image;
this.name = name;
}
@Override
protected String doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//compress the image to jpg format
image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
/*
* encode image to base64 so that it can be picked by saveImage.php file
* */
String encodeImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
//generate hashMap to store encodedImage and the name
HashMap<String,String> detail = new HashMap<>();
detail.put("name", name);
detail.put("image", encodeImage);
try{
//convert this HashMap to encodedUrl to send to php file
String dataToSend = hashMapToUrl(detail);
//make a Http request and send data to saveImage.php file
String response = Request.post(SERVER,dataToSend);
//return the response
return response;
}catch (Exception e){
e.printStackTrace();
Log.e(TAG,"ERROR "+e);
return null;
}
}
@Override
protected void onPostExecute(String s) {
//show image uploaded
Toast.makeText(getApplicationContext(),"Image Uploaded",Toast.LENGTH_SHORT).show();
}
}
}
感謝。
這是您的真實密碼?有了「[」嗎?也許修復第一,然後再試一次,當你嘗試它看看PHP日誌,並張貼你看到的錯誤,如果它不工作。此外,您可能想要顯示用於發佈圖像的android代碼,因爲它看起來不平凡,因爲您希望將其作爲base64編碼的字符串發送。 – Gavriel
對不起。林新的PHP和我一直在看教程,並找到了這個腳本。它有這個「#91 ;;」在裏面。這是什麼意思。我認爲它需要上傳圖像的名稱,圖像將被轉換爲字符串,然後轉換爲Base_64。如果是這樣,應該用什麼來代替「#91 ;;」? –
我不認爲時間戳會在onClick中具有價值。我們仍然不知道什麼是行不通的。代碼在第一眼看起來很好。我修復了你的php。給出錯誤信息,細節... – Gavriel