我正在尝试将从相机捕获的图像保存到服务器。
捕获第一张图片,将其转换为基本64字符串,然后使用Volley请求将其发送到Django后端。在Django方面,我正在使用base64.decodebytes(String Name)
再次将其转换为图像。图像已转换并保存,但未返回成功响应,并且文件未保存在所需的目录中。
我正在使用imagefield使用
将图像保存到给定路径pic= models.ImageField(upload_to="Folder_name")
Android:
用于将图像转换为字符串base64的函数。
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba= new ByteArrayOutputStream( );
bm.compress( Bitmap.CompressFormat.JPEG,100,ba );
byte[] imagebyte = ba.toByteArray();
String encode = Base64.encodeToString(imagebyte, Base64.DEFAULT );
// Toast.makeText( MainActivity.this,"String is"+encode,Toast.LENGTH_LONG ).show();
return encode;
}
使用Volley将参数发送到Django:
protected Map<String, String> getParams() throws AuthFailureError {
String image = getStringImage( photo );
Map<String , String> params = new HashMap<String, String>( );
Date date = new Date( );
long timeMilli = date.getTime();
params.put("IMG", image);
params.put("tm", String.valueOf( timeMilli ));
return params;
Pycharm
URL:
path('add_file', views.fileAdd, name='fil'),
Function DEf:
def fileAdd(request):
pak = request.POST['IMG']
tim = request.POST['tm']
pak = pak.encode('utf-8')
decoded_image_data = base64.decodebytes(pak)
with open(tim+'.JPEG', 'wb') as file_to_save:
file_to_save.write(decoded_image_data)
b = Task(pic=decoded_image_data)
if b.save():
return HttpResponse("Added Successfully")
else:
return HttpResponse("Not Added")
错误:
Internal Server Error: /ams/add_file
Traceback (most recent call last):
File "C:\python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Attique Bhutta\Desktop\alauddin\smartVisit\DsmartVisit\ams\views.py", line 135, in fileAdd
if b.save():
File "C:\python38\lib\site-packages\django\db\models\base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\python38\lib\site-packages\django\db\models\base.py", line 782, in save_base
updated = self._save_table(
File "C:\python38\lib\site-packages\django\db\models\base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\python38\lib\site-packages\django\db\models\base.py", line 924, in _do_insert
return manager._insert(
File "C:\python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\python38\lib\site-packages\django\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\python38\lib\site-packages\django\db\models\sql\compiler.py", line 1383, in execute_sql
for sql, params in self.as_sql():
File "C:\python38\lib\site-packages\django\db\models\sql\compiler.py", line 1326, in as_sql
value_rows = [
File "C:\python38\lib\site-packages\django\db\models\sql\compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\python38\lib\site-packages\django\db\models\sql\compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\python38\lib\site-packages\django\db\models\sql\compiler.py", line 1278, in pre_save_val
return field.pre_save(obj, add=True)
File "C:\python38\lib\site-packages\django\db\models\fields\files.py", line 286, in pre_save
if file and not file._committed:
AttributeError: 'bytes' object has no attribute '_committed'
[28/Feb/2020 11:09:01] "POST /ams/add_file HTTP/1.1" 500 178390
图像已交付并保存到服务器端。但是在服务器上也会产生上述错误。 Python函数返回的响应不是我想要的。
答案 0 :(得分:0)
解决了这个问题。问题出在ImageField
上。我换了
django的功能如下:
def fileAdd(request):
pak = request.POST['IMG']
tim = request.POST['tm']
pak = pak.encode('utf-8')
decoded_image_data = base64.decodebytes(pak)
with open("C:\\Users\\Attique Bhutta\\Desktop\\alauddin\\smartVisit\\DsmartVisit\\assets\\media\\" +tim+'.JPEG', 'wb') as file_to_save:
file_to_save.write(decoded_image_data)
path = "C:\\Users\\Attique Bhutta\\Desktop\\alauddin\\smartVisit\\DsmartVisit\\assets\\media\\" +tim+'.JPEG';
b = Task(imgPath=path)
b.save()
return HttpResponse("Added Successfully")