We have tried this code for android and php. Please see below code.
Android
Scn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Open default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// start the image capture Intent
startActivityForResult(intent, 100);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = intent.getData();
imageBitmap = (Bitmap) intent.getExtras().get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
imageView1.setImageBitmap(imageBitmap);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
submitData(encodedImage);
}
}
public void submitData(final String encodedImage) {
try {
new Thread(new Runnable() {
@Override
public void run() {
try {
//Looper.prepare();
Log.d("looper", "-->>>>");
try
{
isAuthorized = isAuthenticated(encodedImage);
}
catch (Exception e)
{
Log.e("Exception ==> ", e.toString());
}
MHandler.post(new Runnable() {
@Override
public void run() {
try {
if(isAuthorized){
if(AuthenticationResultJSONObject!=null){
String message = AuthenticationResultJSONObject.getString("message");
if(AuthenticationResultJSONObject.getString("status").equalsIgnoreCase("1"))
{
Toast.makeText(ScannerActivity.this,message,Toast.LENGTH_SHORT).show();
}
else if (AuthenticationResultJSONObject.getString("status").equalsIgnoreCase("0"))
{
Toast.makeText(ScannerActivity.this,message,Toast.LENGTH_SHORT).show();
}
}
}
else
{
Toast.makeText(ScannerActivity.this, "Loing unsuccessfull, please try again !", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
Log.e("Exception 146==> ", e.toString());
}
finally
{
//progressdialog.dismiss();
//progress.dismissProgressDialog();
}
}
});
} catch (Exception e) {
Log.e("Exception 153==> ", e.toString());
}
}
}).start();
} catch (Exception e) {
Log.e("Exception 159==> ", e.toString());
}
}
public boolean isAuthenticated(final String encodedImage)
{
isAuthorized = false;
String encoder1 = "";
final String url="http://bestpeopleservice.com/projects/scanner/api/api/api.php?class=login&method=login_data&json=";
String finalurl = "{\"data\":{\"qr_code\":\""+username1+"\",\"user_pass\":\""+password1+"\",\"longitude\":\""+longitude1+"\",\"latitude\":\""+latitude1+"\",\"adderss\":\""+adderss1+"\",\"images\":\""+encodedImage+"\"}}";//
Log.v("267 1111111", "done url -> "+url+finalurl);
try {
encoder1 = URLEncoder
.encode(finalurl,"UTF-8");
Log.v("267 222", "done url 2 -> "+url+encoder1);
}
catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
}
try {
if (isNetworkConnected()) {
AuthenticationResultJSONObject = new JSONObject(doFetchDataFromWebService_Simple_OnlyJSONResponse(url+encoder1));
Log.v("Online", "User json array === "+AuthenticationResultJSONObject);
}else{
Toast.makeText(ScannerActivity.this, "Please check your internet connection and try again.", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
e.getMessage();
}
finally{
if(AuthenticationResultJSONObject!=null){
isAuthorized = true;
}
else
{
isAuthorized=false;
}
}
// }
// });
return isAuthorized;
}
// Checking the internet connection
public boolean isNetworkConnected() {
ConnectivityManager connectivitymanagar = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = connectivitymanagar.getActiveNetworkInfo();
if (networkinfo == null || !networkinfo.isConnectedOrConnecting()) {
return false;
} else {
return true;
}
}
public String doFetchDataFromWebService_Simple_OnlyJSONResponse(
String WebServiceURL) {
String responseBody = "";
JSONObject jobj = new JSONObject();
try {
HttpGet WSHttpPost = null;
HttpClient WSHttpClient = null;
HttpResponse WSHttpResponse = null;
WSHttpClient = getNewHttpClient();
WSHttpPost = new HttpGet(WebServiceURL);
WSHttpResponse = WSHttpClient.execute(WSHttpPost);
Log.d("resp", ""+WSHttpResponse);
//responseBody = EntityUtils.toString(WSHttpResponse.getEntity());
responseBody = EntityUtils.toString(WSHttpResponse.getEntity(), "UTF-8");
Log.v("response1", "" + responseBody);
} catch (Exception e) {
Log.e("Exception2 ==> ", e.toString());
}
if (responseBody == null || responseBody.equalsIgnoreCase("null")) {
return "";
} else {
return responseBody;
}
}
public HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host,
port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
It returns parsing bytestring. Its value is too much long.
PHP
$base=$Data['images'];
$binary=base64_decode($base);
header('Content-Type: bitmaps; charset=utf-8');
header("Content-Length: " . mb_strlen($base, '8bit'));
$file = fopen('qr_code.jpeg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory';
Above code is working. But it have some issue. Image save successfully in folder but image will be displayed as corrupted. How can we resolve this issue.
Thanks in advance.
I'm not and Android developer.
But I know the problem is with your request. You are passing all data in
GET
request method (appending parameters in URL). But It has limitation of 2,083 character length. (It depends on webserver actually)So, If your image size is higher than that, it will simply truncate from URL.
For the solution, you have to use
POST
request to submit data to webservice.Android
Refer answer of this question for android code snippet
PHP code
It will create
file_name_to_create.jpeg
in current directory.Reference
Hi i have used this approach to save myImage.
Create images folder inside your root.
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../images/" . $varFileName);
Use this URL to download my code for sending imagefile to php server.
https://onedrive.live.com/redir?resid=D6874F5E96BBF5A8!17174&authkey=!AIA5xGcRI7UnyNg&ithint=folder%2cjar
Please try this code this code working for me.