如何上传图像到数据库时验证电子邮件 [英] How to verify email when uploading image to database

查看:140
本文介绍了如何上传图像到数据库时验证电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里的新手问题,在一滴的,我下面的教程的形式,以便即时上传图像。我的问题,现在是我要上传我的形象在特定的电子邮件,这意味着我把电子邮件和图像将被上传到数据库中的特定电子邮件。香港专业教育学院完成了上传图像的一部分,但不知道如何把电子邮件。 tqvm先进的。

1.MainActivity.java

 公共类MainActivity扩展AppCompatActivity实现View.OnClickListener {公共静态最后弦乐UPLOAD_URL =htt​​p://10.0.3.2/AndroidImageBlob/upload.php;
公共静态最后弦乐UPLOAD_KEY =形象;
公共静态最后的字符串标记=我的信息;私人INT PICK_IMAGE_REQUEST = 1;私人按钮buttonChoose;
私人按钮buttonUpload;私人的EditText editTextId;私人ImageView的ImageView的;私人位图位图;私人文件路径乌里;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    editTextId =(EditText上)findViewById(R.id.editTextId);    buttonChoose =(按钮)findViewById(R.id.buttonChoose);
    buttonUpload =(按钮)findViewById(R.id.buttonUpload);    ImageView的=(ImageView的)findViewById(R.id.imageView);    buttonChoose.setOnClickListener(本);
    buttonUpload.setOnClickListener(本);
}私人无效showFileChooser(){
    意向意图=新的Intent();
    intent.setType(图像/ *);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(意向,选择图片),PICK_IMAGE_REQUEST);
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);    如果(要求code == PICK_IMAGE_REQUEST&放大器;&安培;结果code == RESULT_OK&放大器;&放大器;数据= NULL&放大器;!&安培;!data.getData()= NULL){        文件路径= data.getData();
        尝试{
            位图= MediaStore.Images.Media.getBitmap(getContentResolver(),文件路径);
            imageView.setImageBitmap(位图);
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}
公共字符串getStringImage(BMP位图){
    ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
    bmp.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
    字节[] = imageBytes baos.toByteArray();
    字符串连接codeDIMAGE = Base64.en codeToString(imageBytes,Base64.DEFAULT);
    返回连接codeDIMAGE;
}私人无效uploadImage(){    类UploadImage扩展的AsyncTask<位图,太虚,字符串> {        ProgressDialog负荷;
        RequestHandler RH =新RequestHandler();        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            装载= ProgressDialog.show(MainActivity.this上传图片,请稍候...,真实,真实);
        }        @覆盖
        保护无效onPostExecute(String s)将{
            super.onPostExecute(多个);
            loading.dismiss();
            Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_LONG).show();
        }        @覆盖
        保护字符串doInBackground(位图... PARAMS){
            位图的位图=参数[0];
            串uploadImage = getStringImage(位图);            HashMap的<字符串,字符串>数据=新的HashMap<>();
            data.put(UPLOAD_KEY,uploadImage);            字符串结果= rh.sendPostRequest(UPLOAD_URL,数据);            返回结果;
        }
    }    UploadImage UI =新UploadImage();
    ui.execute(位图);
}@覆盖
公共无效的onClick(视图v){
    如果(V == buttonChoose){
        showFileChooser();
    }
    如果(V == buttonUpload){
        上传图片();
    }
}
}

2.RequestHandler.java

 公共类RequestHandler {公共字符串sendGetRequest(字符串URI){
    尝试{
        网址URL =新的URL(URI);
        HttpURLConnection的CON =(HttpURLConnection类)url.openConnection();
        的BufferedReader的BufferedReader =新的BufferedReader(新的InputStreamReader(con.getInputStream()));        字符串结果;        StringBuilder的SB =新的StringBuilder();        而((结果= bufferedReader.readLine())!= NULL){
            sb.append(结果);
        }        返回sb.toString();
    }赶上(例外五){
        返回null;
    }
}公共字符串sendPostRequest(字符串requestURL,
                              HashMap的<字符串,字符串> postDataParams){    URL网址;
    串响应=;
    尝试{
        URL =新的URL(requestURL);        HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod(POST);
        conn.setDoInput(真);
        conn.setDoOutput(真);
        OutputStream的OS = conn.getOutputStream();
        但是BufferedWriter作家=新的BufferedWriter(
                新OutputStreamWriter(OS,UTF-8));
        writer.write(getPostDataString(postDataParams));        writer.flush();
        writer.close();
        os.close();
        INT响应code = conn.getResponse code();        如果(响应code == HttpsURLConnection.HTTP_OK){
            BR的BufferedReader =新的BufferedReader(新的InputStreamReader(conn.getInputStream()));
            响应= br.readLine();
        }其他{
            响应=错误登记;
        }
    }赶上(例外五){
        e.printStackTrace();
    }    返回响应;
}私人字符串getPostDataString(HashMap的<字符串,字符串> PARAMS)抛出UnsupportedEncodingException {
    StringBuilder的结果=新的StringBuilder();
    布尔第一= TRUE;
    对于(Map.Entry的<字符串,字符串>项:params.entrySet()){
        如果(第一)
            第一= FALSE;
        其他
            result.append(与&);        result.append(URLEn coder.en code(entry.getKey(),UTF-8));
        result.append(=);
        result.append(URLEn coder.en code(entry.getValue(),UTF-8));
    }    返回result.toString();
}
}

3.upload.php

 < PHP 如果($ _ SERVER ['REQUEST_METHOD'] =='POST'){ $用户名= $ _ POST [用户名];
 $图像= $ _ POST ['形象']; require_once('dbConnect.php'); $ SQL =INSERT INTO android_image(图)值,其中用户名=(?)'$用户名。'; $语句= mysqli_ prepare($ CON,$ SQL); mysqli_stmt_bind_param($语句,S,$图像);
 mysqli_stmt_execute($语句); $检查= mysqli_stmt_affected_rows($语句);     如果($检查== 1)
     {
     回声图片上载成功;
     }
     其他
     {
     回声错误上传影像;
     }
     mysqli_close($ CON);
 }
 其他
 {
 回声错误;
 }


解决方案

在您的Hashmap添加它象下面这样:

 的HashMap<字符串,字符串>数据=新的HashMap<>();
data.put(UPLOAD_KEY,uploadImage);
data.put(ID,[你的ID值]);

和在服务器端,你可以得到的 ID 使用值 $ _ POST [身份证] 如果您使用的是PHP。

newbie question here, so im uploading image in the form of blob which i follows a tutorial. my problem right now is i want to upload my image on specific email which means i put in the email and the image will be uploaded to that specific email in the database. ive done the uploading image part but have no idea how to put the email. tqvm in advanced.

1.MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final String UPLOAD_URL = "http://10.0.3.2/AndroidImageBlob/upload.php";
public static final String UPLOAD_KEY = "image";
public static final String TAG = "MY MESSAGE";

private int PICK_IMAGE_REQUEST = 1;

private Button buttonChoose;
private Button buttonUpload;

private EditText editTextId;

private ImageView imageView;

private Bitmap bitmap;

private Uri filePath;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextId = (EditText) findViewById(R.id.editTextId);

    buttonChoose = (Button) findViewById(R.id.buttonChoose);
    buttonUpload = (Button) findViewById(R.id.buttonUpload);

    imageView = (ImageView) findViewById(R.id.imageView);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);
}

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

private void uploadImage(){

    class UploadImage extends AsyncTask<Bitmap,Void,String> {

        ProgressDialog loading;
        RequestHandler rh = new RequestHandler();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this, "Uploading Image", "Please wait...", true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Bitmap...params) {
            Bitmap bitmap = params[0];
            String uploadImage = getStringImage(bitmap);

            HashMap<String,String> data = new HashMap<>();
            data.put(UPLOAD_KEY, uploadImage);

            String result = rh.sendPostRequest(UPLOAD_URL,data);

            return result;
        }


    }

    UploadImage ui = new UploadImage();
    ui.execute(bitmap);
}

@Override
public void onClick(View v) {
    if (v == buttonChoose) {
        showFileChooser();
    }
    if(v == buttonUpload){
        uploadImage();
    }
}
}

2.RequestHandler.java

public class RequestHandler {

public String sendGetRequest(String uri) {
    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String result;

        StringBuilder sb = new StringBuilder();

        while((result = bufferedReader.readLine())!=null){
            sb.append(result);
        }

        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

public String sendPostRequest(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            response = br.readLine();
        } else {
            response = "Error Registering";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

private String getPostDataString(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();
}
}

3.upload.php

<?php

 if($_SERVER['REQUEST_METHOD']=='POST'){

 $username = $_POST['username'];
 $image = $_POST['image'];

 require_once('dbConnect.php');

 $sql = "INSERT INTO android_image (image) VALUES (?) WHERE username  ='".$username."'";

 $stmt = mysqli_prepare($con,$sql);

 mysqli_stmt_bind_param($stmt,"s",$image);
 mysqli_stmt_execute($stmt);

 $check = mysqli_stmt_affected_rows($stmt);

     if($check == 1)
     {
     echo "Image Uploaded Successfully";
     }
     else
     {
     echo "Error Uploading Image";
     }
     mysqli_close($con);
 }
 else
 {
 echo "Error";
 }

解决方案

Add it in your Hashmap like below:

HashMap<String,String> data = new HashMap<>();
data.put(UPLOAD_KEY, uploadImage);
data.put("id", [your value for the id]);

And on the server side, you can get the value for the id using $_POST["id"] if you are using PHP.

这篇关于如何上传图像到数据库时验证电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆