بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
آپلود عکس در دیتابیس لوکال؟ (حل نشده)
سلام
برای آپلود کردن عکس از داخل برنامه به دیتابیس لوکال چه راهی رو پیشنهاد میکنید؟ (آیا استاد این روش رو آموزش دادند؟) میدونم که باید آدرس عکس رو داخل دیتابیس ذخیره کنم اما خود عکس کجا ذخیره میشه؟ و به طور کلی اگر کد اینکار رو لطف کنید بسیار سپاسگذارم.
برای این سوال 1 پاسخ وجود دارد.

پاسخ به سوال
احمد
8 سال پیش
+1
0
سلام کد قسمت سرور (php) اینه:
<?php header('content-type: bitmap; charset=utf-8'); if(isset($_REQUEST["encoded_string"])) { $encoded_string = $_REQUEST["encoded_string"]; $image_name = $_REQUEST["image_name"]; $decoded_string = base64_decode($encoded_string); $path = 'images/'.$image_name; $file = fopen($path, 'wb'); $is_written = fwrite($file, $decoded_string); fclose($file); if($is_written > 0) { $connection = mysqli_connect('localhost','root','','dbname'); $query = "INSERT INTO db_photo (photo_name,photo_path) VALUES ('$image_name','$path');"; $result = mysqli_query($connection,$query); if($result) { echo "success"; } else { echo "failed"; } mysqli_close($connection); } } ?>
به وسیله این کد می تونی تصویری که به صورت base64 از سمت اندروید ارسال میشه را در هاست ذخیره میکنه و ادرس فایل را در دیتابس می نویسه
کد sql جدول تصاویر:
CREATE TABLE IF NOT EXISTS `db_photo` ( `photo_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `photo_name` varchar(30) COLLATE utf8_persian_ci NOT NULL, `photo_path` varchar(50) COLLATE utf8_persian_ci NOT NULL, PRIMARY KEY (`photo_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=35 ;
سمت اندروید:
public void uploadImage(View v) {
// When Image is selected from Gallery
if (imgPath != null && !imgPath.isEmpty()) {
prgDialog.setMessage("Converting Image to Binary Data");
prgDialog.show();
// Convert image to String using Base64
encodeImagetoString();
// When Image is not selected from Gallery
} else {
Toast.makeText(
getApplicationContext(),
"You must select image from gallery before you try to upload",
Toast.LENGTH_LONG).show();
}
}
//**************************************************************************
public void encodeImagetoString() {
new AsyncTask<Void, Void, String>() {
protected void onPreExecute() {
};
@Override
protected String doInBackground(Void... params) {
BitmapFactory.Options options = null;
options = new BitmapFactory.Options();
options.inSampleSize = 3;
bitmap = BitmapFactory.decodeFile(imgPath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload
// easy
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
return "";
}
@Override
protected void onPostExecute(String msg) {
prgDialog.setMessage("Calling Upload");
// Put converted Image string into Async Http Post param
params.put("encoded_string", encodedString);
// Trigger Image upload
triggerImageUpload();
}
}.execute(null, null, null);
}
public void triggerImageUpload() {
makeHTTPCall();
}
public void makeHTTPCall() {
prgDialog.setMessage("Invoking Php");
AsyncHttpClient client = new AsyncHttpClient();
// Don't forget to change the IP address to your LAN address. Port no as
// well.
client.post("http://localhost/app/upload_image.php", params,
new AsyncHttpResponseHandler() {
// When the response returned by REST has Http
// response code '200'
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
/* Toast.makeText(getApplicationContext(), response,
Toast.LENGTH_LONG).show();*/
if (response.equalsIgnoreCase(success)) {
fileUploadSuccess(btnuploadbanner_ads);
} else if (response.equalsIgnoreCase(failed)) {
fileUploadError(btnuploadbanner_ads);
}
}
// When the response returned by REST has Http
// response code other than '200' such as '404',
// '500' or '403' etc
@Override
public void onFailure(int statusCode, Throwable error,
String content) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getApplicationContext(),
"Requested resource not found",
Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(
getApplicationContext(),
uploadservererror + " " + statusCode, Toast.LENGTH_LONG)
.show();
fileUploadError(btnuploadbanner_ads);
}
}
});
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .