2014-12-16 2 views
0

사진을 여러 장 찍고 완료하면 장치에 보관하지 않고 WebService로 모두 보내고 싶습니다.Android : WebService로 사진을 보내십시오.

이미 전송 후 삭제를 시도했지만 성공하지 못했습니다.

가장 좋은 방법은 무엇입니까?

이것은 내 이미지 캡처 활동입니다.

private ImageView imgPreview; 
private Button btnCapturePicture; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.image_capture); 

    context = getApplicationContext(); 
    imgPreview = (ImageView) findViewById(R.id.imgPreview); 
    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture); 

    /** 
    * Capture image button click event 
    * */ 
    btnCapturePicture.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // capture picture 
      captureImage(); 
     } 
    }); 

} 


GPSTracker gps; 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // if the result is capturing Image 
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
     //TODO 
     gps = new GPSTracker(ImageCaptureActivity.this); 
     if (resultCode == RESULT_OK) { 
      // successfully captured the image 
      // display it in image view 
      previewCapturedImage(); 

      //get gps location coordinates 
      if(gps.canGetLocation()) 
      { 
       double latitude = gps.getLatitude(); 
       double longitude = gps.getLongitude(); 

       Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " 
         + longitude, Toast.LENGTH_LONG).show(); 

       //from latitude and longitude to address 
       Geocoder gcd = new Geocoder(context, Locale.getDefault()); 
       List<Address> addresses = null; 
       try { 
        addresses = gcd.getFromLocation(latitude, longitude, 1); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       if (addresses.size() > 0) { 
        String tara = addresses.get(0).getCountryName(); 
        String judet = addresses.get(0).getLocality(); 
        String oras = addresses.get(0).getSubLocality(); 
        String adresa = addresses.get(0).getAddressLine(0); 
        System.out.println(tara + ", " + judet + ", " + oras + ", " + adresa + "\n"); 

       } 
      } else { 
       // Can't get location. 
       // GPS or network is not enabled. 
       // Ask user to enable GPS/network in settings. 
       Toast.makeText(getApplicationContext(), "Turn on GPS", Toast.LENGTH_LONG).show(); 
       Intent backIntent = new Intent(ImageCaptureActivity.this, Cont.class); 
       startActivity(backIntent); 
      } 


      //TODO 
      //GET DATE OF IMAGE 
      String pathToFile = fileUri.getPath(); 
      File file = new File(pathToFile); 
      if(file.exists()) { 
       String date = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(
         new Date(file.lastModified()) 
       ); 
       Toast.makeText(context, date, Toast.LENGTH_LONG) 
         .show(); 
      } 



     } else if (resultCode == RESULT_CANCELED) { 
      // user cancelled Image capture 
      Toast.makeText(getApplicationContext(), 
        "Cancelled", Toast.LENGTH_SHORT) 
        .show(); 
     } else { 
      // failed to capture image 
      Toast.makeText(getApplicationContext(), 
        "Error!", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 

} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    // save file url in bundle as it will be null on scren orientation 
    // changes 
    outState.putParcelable("file_uri", fileUri); 
} 

/* 
* Here we restore the fileUri again 
*/ 
@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    // get the file url 
    fileUri = savedInstanceState.getParcelable("file_uri"); 
} 

그리고 도우미 방법 :

private void captureImage() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 

} 

/** 
* Creating file uri to store image/video 
*/ 
public Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/* 
* returning image/video 
*/ 
private static File getOutputMediaFile(int type) { 

    // External sdcard location 
    File mediaStorageDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
      IMAGE_DIRECTORY_NAME); 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d(IMAGE_DIRECTORY_NAME, "file" 
        + IMAGE_DIRECTORY_NAME + " was not created"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
      Locale.getDefault()).format(new Date()); 
    File mediaFileName; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFileName = new File(mediaStorageDir.getPath() + File.separator 
       + "IMG_" + timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFileName; 
} 

/* 
* Display image from a path to ImageView 
*/ 
private void previewCapturedImage() { 
    try { 
     imgPreview.setVisibility(View.VISIBLE); 

     // bimatp factory 
     BitmapFactory.Options options = new BitmapFactory.Options(); 

     // downsizing image as it throws OutOfMemory Exception for larger 
     // images 
     options.inSampleSize = 8; 

     final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
       options); 

     imgPreview.setImageBitmap(bitmap); 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
} 

나는 REST WebService에에 사진을 보내려고합니다.

+0

사람들이 도움을 줄 수 있도록 코드를 게시하면 매우 유용합니다. 귀하의 질문은 너무 일반적입니다. –

+0

@ 키릴 _edited_ – Juvie22

답변

0

편집 해 주셔서 감사합니다.

먼저 이미지를 저장하고 메모리에서 이미지를 유지하는 것이 좋지 않기 때문에 삭제하는 것이 좋습니다.

둘째, 웹 서버에서 실행중인 웹 REST 서비스가 있다고 가정합니다. 따라서 이미지를 업로드하려면 서버가 제공하는 엔드 포인트에 대한 HTTP 호출과 적절한 요청 메소드를 사용해야합니다. 정확히 정확한 코드 예제를 보려면 here을 참조하십시오.

마지막으로 전경 서비스에서 무거운 업로드 작업을 수행하여 OS가 업로드 프로세스 중에 스레드를 죽이지 않도록 할 것을 권장합니다.

관련 문제