doumou1864 2015-08-31 16:44
浏览 60

想要存储图像路径

I'm trying to make an app where the registered user has to upload a profile pic. After that, when the user logs in along with his other credentials the image of the user should also get displayed. I also want to pass the path of the image to the table in MySql. My image is stored ina folder in the server while the user details are stored in the table in the MySql. So when the user Logins along with the credentials the image should also get displayed. But when I'm tryin to register I'm getting an error and the image is not getting uploaded. And my app is getting crashed.

PHP file:

<?php

  $con=mysqli_connect("mysql8.000webhost.com","a6391170_root","root123","a6391170_sanjay");

  $name = $_POST["name"];
  $age = $_POST["age"];
  $username = $_POST["username"];
  $password = $_POST["password"];
  $name1=$_POST["name1"];


  $imagepath="pictures1/" . $name1 . ".JPG";

  $statement = mysqli_prepare($con, "INSERT INTO `user1` (name, age, username, password,name1,imagepath) VALUES (?, ?, ?, ?,?,?)");
  mysqli_stmt_bind_param($statement, "sissss", $name, $age, $username, $password,$name1,$imagepath);
  mysqli_stmt_execute($statement);

  mysqli_stmt_close($statement);

  mysqli_close($con);

?>

JAVA FILE:

           UploadImage ui=new UploadImage(image,etName1.getText().toString());
            ui.execute();
            User user = new User(name, age, username, password,name1);
            registerUser(user);
            break;


        case R.id.upImage:
            Intent i=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i,RESULT_IMAGE);

            Toast.makeText(getApplicationContext(),"Selecting Image",Toast.LENGTH_LONG).show();
            break;

    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==RESULT_IMAGE && resultCode==RESULT_OK && data!=null)
    {
        Uri selectedImage=data.getData();
        imageUp.setImageURI(selectedImage);


        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        // Get the cursor
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        // Move to first row
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        imgPath = cursor.getString(columnIndex);
        cursor.close();







        Toast.makeText(getApplicationContext(),"Image Selected",Toast.LENGTH_LONG).show();
    }
    }


private class  UploadImage extends AsyncTask<Void, Void, Void>
{
    Bitmap image;
    String name1;



    public UploadImage(Bitmap image, String name1)
    {

        this.image=image;
        this.name1=name1;

    }



    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected  Void doInBackground(Void... params) 
    {
    System.out.println("here"); 
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
        String encodedImage= Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

        ArrayList<NameValuePair> dataToSend=new ArrayList<NameValuePair>();
        dataToSend.add(new BasicNameValuePair("image",encodedImage));
        dataToSend.add(new BasicNameValuePair("name1",name1));



        HttpParams httpRequestParams=getHttpRequestParams();
        HttpClient client= new DefaultHttpClient(httpRequestParams);
        HttpPost post=new HttpPost(SERVER_ADDRESS + "upload.php");



        try
        {
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);


        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {   
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);

        Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
    }

}

private HttpParams  getHttpRequestParams()
{


    HttpParams httpRequestParams=new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpRequestParams,1000*30);
    HttpConnectionParams.setSoTimeout(httpRequestParams,1000*30);
    return httpRequestParams;
}
  • 写回答

1条回答 默认 最新

  • doulun7739 2015-08-31 17:09
    关注

    This may help you

        <?
    $con=mysqli_connect("mysql8.000webhost.com","a6391170_root","root123","a6391170_sanjay");
    
    $name = $_POST["name"];
    $age = $_POST["age"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    $UploadedFileName=$_FILES['name1']['name'];
    if($UploadedFileName!='')
    {
                    $upload_directory = "pictures1/";
                    $imagepath=$upload_directory.$UploadedFileName;
                    if(move_uploaded_file($_FILES['name1']['tmp_name'], $upload_directory.$UploadedFileName))
                    {    
                         $statement = mysqli_prepare($con, "INSERT INTO `user1`(name, age, username, password,name1,imagepath) 
                                VALUES(?, ?, ?, ?, ?, ?) ");
                         mysqli_stmt_bind_param($statement, "sissss", $name, $age, $username, $password,$UploadedFileName,$imagepath);
                         mysqli_stmt_execute($statement);
                         mysqli_stmt_close($statement);
                    }
    }
    
    mysqli_close($con);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题