我想通过使用我的应用程序将我最新的pic捕获相机保存到mysql数据库中

时间:2016-08-02 06:54:51

标签: android

下面是我的MainActivity.java,其中捕获图像。我已经创建了一个用于将数据保存到数据库中的类,它是(SendPostReqAsyncTask),它扩展了AsyncTask。

public class MainActivity extends AppCompatActivity 
         {
            Button button;
            ImageView iv;
            public ImageView imageId;
            public ArrayList<String> imgFilePath = new ArrayList<String> ();
            public String imgKey;
            private static final int CAMERA_REQUEST = 1888;
            private Uri fileUri;

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

            public void onImageclick()
              {
                final ImageView iv= (ImageView)findViewById(R.id.imageview);
                Button button = (Button)findViewById(R.id.imageclick);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        imageId = iv;
                        imgKey  = "iv";
                        clickpic();
                    }
                });
            }
            private void clickpic() {
                // Check Camera
               if (getApplicationContext().getPackageManager().hasSystemFeature(
                     PackageManager.FEATURE_CAMERA)) 
                 {
                    // Open default camera
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);

                    // start the image capture Intent
                    startActivityForResult(intent, CAMERA_REQUEST);
                } 
                 else 
                  {
                    Toast.makeText(getApplication(), "Camera not supported",
                            Toast.LENGTH_LONG).show();
                  }
            }

    protected void onActivityResult(int requestCode, int resultCode, Intentdata)                     {
      if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
        {
           Bitmap photo = (Bitmap) data.getExtras().get("data");
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String dateTime = sdf.format(Calendar.getInstance().getTime());

        Bitmap dest = Bitmap.createBitmap(photo.getWidth(),photo.getHeight(),
         Bitmap.Config.ARGB_8888);

                    Canvas cs = new Canvas(dest);
                    Paint tPaint = new Paint();
                    tPaint.setTextSize(10);
                    tPaint.setColor(Color.BLUE);
                    tPaint.setStyle(Paint.Style.FILL);
                    cs.drawBitmap(photo, 0f, 0f, null);
                    float height = tPaint.measureText("yY");
                    cs.drawText(dateTime, 2f, height + 5f, tPaint);

                    imageId.getLayoutParams().height = 250;
                    imageId.setImageBitmap(dest);

                    Uri uri = getImageUri(getApplicationContext(), dest);
                    imgFilePath.add(getRealPathFromURI(uri));
                    File finalFile = new File(getRealPathFromURI(uri));
                }
            }


    public Uri getImageUri(Context inContext, Bitmap inImage) 
      {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path =
    MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);

         return Uri.parse(path);
       }

    public String getRealPathFromURI(Uri uri) 
     {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

       return cursor.getString(idx);
     }

       public void onbuttonclick()
        {
            button = (Button) findViewById(R.id.onsubmit);
            button.setOnClickListener(new View.OnClickListener()
             {
                @Override
                public void onClick(View view) {
      SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(MainActivity.this);
                    sendPostReqAsyncTask.execute(locate1);

              }
           });
       }

        }

下面是我的“SendPostReqAsyncTask.java”类...这个类用于将值保存到数据库中..我想使用存储在params [0]或任何参数中的“SendPostReqAsyncTask”类中的最新图像[我]以便它很容易存储在数据库中。我不知道如何在SendPostReqAsyncTask中使用MainActivity的图像。如果有人知道请帮助

public class SendPostReqAsyncTask extends AsyncTask<String, Void ,String> {

    Context context;
    AlertDialog alertDialog;

    SendPostReqAsyncTask(Context ctx)
     {
       context = ctx;
     }
    @Override
    protected String doInBackground(String... params)
      {

        String stringuser = params[0];
        String stringlocation = params[1];

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name",stringlocation));
        nameValuePairs.add(new BasicNameValuePair("gender", stringuser));

   try 
    {
      HttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost("http://something.com/insert-db.php");
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpResponse response = httpClient.execute(httpPost);
      HttpEntity entity = response.getEntity();
    } 
    catch (ClientProtocolException e)
        {

        } catch (IOException e) 
         {

         }
        return "success";
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values)
    {
        super.onProgressUpdate(values);
    }
}

0 个答案:

没有答案