Pages

Saturday, May 31, 2014

Image pass in Json Android

package com.example.fb;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;





import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Test extends Activity implements OnClickListener{
EditText email,pass;
Button btn,btn1,btn2;
ArrayList<NameValuePair> np;
ProgressDialog pd;
JSONParser jp;
String Status;
AlertDialogManager alert;
String pathMaster ="";
byte[] byteImage1 = null;
String byteImage;
String Image;
Bitmap bmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);

alert = new AlertDialogManager();

/*email = (EditText)findViewById(R.id.editresetemail);
pass = (EditText)findViewById(R.id.editresetpass);*/
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
btn1 = (Button)findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2 = (Button)findViewById(R.id.button3);
btn2.setOnClickListener(this);

jp = new JSONParser();
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub


if(v == btn){

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   // request code

   startActivityForResult(cameraIntent, 99);
}
if(v == btn1){

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 0);
}
if(v == btn2){

getImage1();
new insert().execute();
}

}

private void getImage1() {
// TODO Auto-generated method stub

ByteArrayOutputStream bao = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    byteImage1 = bao.toByteArray();
    Image=Base64.encodeToString(byteImage1, Base64.DEFAULT);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(resultCode == RESULT_OK && requestCode==99)
{
Uri targetUri = data.getData();
pathMaster = getRealPathFromURI(targetUri);

bmp = ShrinkBitmap(pathMaster, 100, 100);
        ImageView image1 = (ImageView)findViewById(R.id.image);
        image1.setImageBitmap(bmp);

}
else if(resultCode == RESULT_OK && requestCode==0)
{
Uri targetUri = data.getData();
        pathMaster = getRealPathFromURI(targetUri).toString();            
        bmp = ShrinkBitmap(pathMaster, 100, 100);
        ImageView image1 = (ImageView)findViewById(R.id.image);

        image1.setImageBitmap(bmp);
    }  
super.onActivityResult(requestCode, resultCode, data);
}


/*
************************ Compress Image **************************
*/
private Bitmap ShrinkBitmap(String path, int width, int height) {
// TODO Auto-generated method stub

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, bmpFactoryOptions);

int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

if (heightRatio > 1 || widthRatio > 1)
{
 if (heightRatio > widthRatio)
 {
  bmpFactoryOptions.inSampleSize = heightRatio;
 } else {
  bmpFactoryOptions.inSampleSize = widthRatio;
 }
}

bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, bmpFactoryOptions);
return bitmap;


}

/*
*************** Get path of image from camera or gallery *******************
*/

private String getRealPathFromURI(Uri targetUri) {
// TODO Auto-generated method stub
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( targetUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}




class insert extends AsyncTask<Void, Void, Void>{





@Override
protected void onPreExecute() {
// TODO Auto-generated method stub

pd = new ProgressDialog(Test.this);
pd.setMessage("Loading....");
pd.setTitle("Update Password");
pd.show();

super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {



np = new ArrayList<NameValuePair>();
np.add(new BasicNameValuePair("session_id","85"));
np.add(new BasicNameValuePair("ns_data",Image));



JSONObject jo = jp.makeHttpRequest(Constant.PROFILEPIC, "POST", np);



// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
pd.dismiss();



super.onPostExecute(result);
}



}



}

No comments:

Post a Comment