2014-01-23 11 views
0

我有一個android應用程序發送圖像到我的數據庫;Android應用程序發送圖像到MySQL

public class NewProductActivity extends Activity { 

// Progress Dialog 
private ProgressDialog pDialog; 
public String image_str; 
JSONParser jsonParser = new JSONParser(); 
EditText inputName; 
EditText inputPrice; 
EditText inputDesc; 
EditText inputImg; 
Button btnTakePhoto; 
ImageView imgTakenPhoto; 
private static final int CAM_REQUREST = 1313; 
// url to create new product 
private static String url_create_product = "http://buiud.com/android_connect/create_product.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.add_product); 

    // Edit Text 
    inputName = (EditText) findViewById(R.id.inputName); 
    inputPrice = (EditText) findViewById(R.id.inputPrice); 
    inputDesc = (EditText) findViewById(R.id.inputDesc); 
    //inputImg = (EditText) findViewById(R.id.imageView1); 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);   
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
    byte[] byte_arr = stream.toByteArray(); 
    image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 


    // Create button 
    Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct); 

    // button click event 
    btnCreateProduct.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      // creating new product in background thread 
      new CreateNewProduct().execute(); 
     } 
    }); 
    btnTakePhoto = (Button) findViewById(R.id.button1); 
    imgTakenPhoto = (ImageView) findViewById(R.id.imageView1); 

    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker()); 

} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == CAM_REQUREST) { 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      imgTakenPhoto.setImageBitmap(thumbnail); 
     } 
} 

class btnTakePhotoClicker implements Button.OnClickListener 
{ 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAM_REQUREST); 
    } 
} 
/** 
* Background Async Task to Create new product 
* */ 
class CreateNewProduct extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(NewProductActivity.this); 
     pDialog.setMessage("Creating Product.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    /** 
    * Creating product 
    * */ 
    protected String doInBackground(String... args) { 
     String name = inputName.getText().toString(); 
     String price = inputPrice.getText().toString(); 
     String description = inputDesc.getText().toString(); 
     //String image_str = inputImg.getText().toString(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("name", name)); 
     params.add(new BasicNameValuePair("price", price)); 
     params.add(new BasicNameValuePair("description", description)); 
     params.add(new BasicNameValuePair("img",image_str)); 
     //params.add(new BasicNameValuePair("image", image)); 

     // getting JSON Object 
     // Note that create product url accepts POST method 
     JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
       "POST", params); 

     // check log cat fro response 
     Log.d("Create Response", json.toString()); 

     // check for success tag 
     try { 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // successfully created product 
       Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
       startActivity(i); 

       // closing this screen 
       finish(); 
      } else { 
       // failed to create product 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once done 
     pDialog.dismiss(); 
    } 

} 
} 

甚至以爲我想要的形象被.png格式它出現在數據庫中的.bin,我怎樣才能改變這種狀況,我看了一下打字的東西時,應用程序被調用create.php但沒有雪茄。我甚至不能在HTML頁面上顯示.bin作爲圖像。

+0

可以使用HttpPost上傳圖片嗎? –

+0

也許這就是爲什麼該應用程序會在上傳後立即崩潰? – raklar

+0

請發佈您的logcat。 –

回答

0

由於您的圖片是在Base64中編碼的,您需要在服務器端解碼並將其保存爲PNG文件。

<?php 
$decoded=base64_decode($encodedString);  
file_put_contents('newImage.PNG',$decoded); 
?> 

http://php.net/fr/base64_decode

+0

$ encodeString是我的MySQL變量,它會是$ img嗎?當我正在更新我的數據庫或從數據庫中檢索時,這也用到了嗎?非常感謝! – raklar

+0

似乎你正在將字符串endoded圖像存儲在Mysql中,對嗎? 然後Mysql將它存儲爲二進制文件是正常的,您的工作是在將其顯示爲PNG圖像之前解碼該二進制文件。 – kylexy1357

0
<?php 
    $name = $_POST['image']; 
    $entry = base64_decode($name); 
    $image = imagecreatefromstring($entry); 
    $directory = dirname(__FILE__).DIRECTORY_SEPARATOR."images/".DIRECTORY_SEPARATOR."index".$title.".jpeg"; 
    header ('Content-type:image/jpeg'); 
    $imagetojpg=imagejpeg($image, $directory); 
    imagedestroy($image);  
    readfile ($directory); 
    exit(); 
    ?> 

這應該工作fine.image是包含編碼字符串的base64的變量。

相關問題