Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:10:55+00:00 2026-05-25T17:10:55+00:00

i want to upload my image data(file) and string data(username) from android to mysql

  • 0

i want to upload my image data(file) and string data(username) from android to mysql database on the server

my JSON code :

private void uploadFile() {
        // TODO Auto-generated method stub
        String nama = getIntent().getStringExtra("user");
        Bitmap bitmapOrg= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Chart1.png");
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte [] ba = bao.toByteArray();
        String ba1=Base64.encodeBytes(ba);
        ArrayList nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("file",ba1));
        nameValuePairs.add(new BasicNameValuePair("username",nama));
        try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new
        HttpPost("http://ipadress/base.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse httpRespose = httpclient.execute(httppost);
       HttpEntity httpEntity = httpRespose.getEntity();
       InputStream in = httpEntity.getContent();
       BufferedReader read = new BufferedReader(new InputStreamReader(in));

       String isi= "";
       String baris= "";

       while((baris = read.readLine())!=null){
          isi+= baris;
       }

           //Jika isi tidak sama dengan "null " maka akan tampil Toast "Register Success" sebaliknya akan tampil "Register Failure"
           if(!isi.equals("null")){                  
               Toast.makeText(this, "Register Success", Toast.LENGTH_LONG).show();
           }else{
               Toast.makeText(this, "Register Failure", Toast.LENGTH_LONG).show();
           }

        }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        }

  }

and my php code :

<?php
include_once("koneksi.php");

$username = $_REQUEST['username'];
$file = $_REQUEST['file'];

$hasil = mysql_query("select (max(ID)+1)as newid  from userownfile"); 
$row = mysql_fetch_row($hasil); 

$base = $_REQUEST['file'];
$filename = $row[0] . ".jpg";
$buffer=base64_decode($base);
$path = "img/".$filename.".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn=mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");

if($username && $file){
$sql = "insert into userownfile(username,file) values('$username','" . 

$path . "')";
mysql_query($sql);
}

$string= "select * from userownfile";
$my_string= mysql_query($string);
if($my_string){
   while($object= mysql_fetch_assoc($my_string)){
      $output[] = $object;
   }

   echo json_encode($output);

?>

my table on the database should be like this :

ID | username | file

but i when i tried to upload, image and username didn’t inserted on my database, am i missing something? anyone can help me to fix the code? thanks before

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:10:56+00:00Added an answer on May 25, 2026 at 5:10 pm

    Use something like Fiddler to ensure that the packet you sent to the server is the packet the server is expecting.

    If you want to format your text as JSON, I’d advise you use the built in classes and do something like:

    JSONObject json = new JSONObject();
    json.put("file", ba1);
    json.put("username", nama);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send iPhone image to server database. I wrote following code. I
Hallo I want to make an file upload. The script should take the image,
I want to upload a list of users from my work's LDAP server to
i want to upload the sound file (.ogg) in server. But it should write
I simply want to upload an image to a server with POST. As simple
I've got a simple upload form. Here's my code: <form id=Form1 method=post enctype=multipart/form-data runat=server
I want use from multiple upload image in codeigniter but problem is here that
How to do the following: I want to have a link [upload image] which
Let's say i have an image uploader script, i want to prevent the upload
I want to upload and then process a file in a Ruby on Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.