如何为android图像添加唯一名称
当我将图像上传到服务器和文件夹以保存它时,我尝试将唯一的名称作为一个数字添加到图像中。 我只需要使用数字,而不是重复的数字。我尝试了不同的方法,但没有成功。现在如果我上传图像,名称将与前一个相同,这将使前一个图像从文件夹中删除。 如果有人知道解决办法,请帮助我 我使用以下代码:如何为android图像添加唯一名称,android,Android,当我将图像上传到服务器和文件夹以保存它时,我尝试将唯一的名称作为一个数字添加到图像中。 我只需要使用数字,而不是重复的数字。我尝试了不同的方法,但没有成功。现在如果我上传图像,名称将与前一个相同,这将使前一个图像从文件夹中删除。 如果有人知道解决办法,请帮助我 我使用以下代码: private void upload() { StringRequest stringRequest = new StringRequest(Request.Method.POST, u
private void upload() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, upload_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String Response = jsonObject.getString("response");
Toast.makeText(Add.this, Response, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
if (bitmap != null)
{
params.put("image", imageToString(bitmap));
params.put("webformatURL", img_location + "/" + textfrom.getText().toString().trim() + ".jpg");
}
params.put("FROM", textfrom.getText().toString().trim());
return params;
}
};
RequestQueue requestQueue = (RequestQueue) Volley.newRequestQueue(Add.this);
requestQueue.add(stringRequest);
}
私有无效上载(){
StringRequest StringRequest=新建StringRequest(Request.Method.POST,upload\u url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
字符串响应=jsonObject.getString(“响应”);
Toast.makeText(Add.this,Response,Toast.LENGTH_LONG).show();
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
if(位图!=null)
{
参数put(“图像”,imageToString(位图));
参数put(“webformatURL”,img_location+“/”+textfrom.getText().toString().trim()+”.jpg”);
}
put(“FROM”,textfrom.getText().toString().trim());
返回参数;
}
};
RequestQueue RequestQueue=(RequestQueue)Volley.newRequestQueue(Add.this);
添加(stringRequest);
}
和php文件
<?php
$servername = "localhost";
$username = "localhost";
$password = "localhost";
$dbname = "localhost";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn) {
$image = $_POST["image"];
$webformatURL = $_POST["webformatURL"];
$FROM = $_POST["FROM"];
$date = date('Y-m-d');
$sql = "insert into image(FROM,webformatURL) values('$FROM','$webformatURL')";
$upload_path = "uploads/$FROM.jpg";
if(mysqli_query($conn, $sql))
{
file_put_contents($upload_path, base64_decode($image));
echo json_encode(array('response' => 'Image Upload Successfully'));
} else {
echo json_encode(array('response' => 'Image Upload Failed'));
}
} else {
echo json_encode(array('response' => 'Connection Error'));
}
mysqli_close($conn);
?>
我的名字如下
String fileName = String.format(Locale.US, "%d.jpg", System.currentTimeMillis());
简单实用。以毫秒为单位获取日期当我尝试获取日期时,它在数据库中带有日期名称,但在文件夹中只有日期视图,以便在我的应用程序中在Recycleriew中立即查看照片。出于这个原因,我只想添加数字数据,因为时间戳只包含数字。你的PHP非常糟糕。您需要学习如何正确编写PHP,不要使用您编写的代码。这里有一些很好的文章:在我的代码中哪里可以应用这些?对不起,我是一个新的应用程序开发人员,没有什么经验experience@marwan你要为一个文件或图像设置唯一的名称。我会试试,告诉你这个数字可以在一段时间后重复吗?