Thursday, January 16, 2014

How to Save URL Image as JPEG and MMS on Android (java)

This takes a static image URL from the internet, saves it as a JPEG on your internal memory, then composes an MMS message and attached it. I've taken the URLs from Google Maps API, Mapquest, and Bing Maps API. Please take a look at the java code below. This of course has to be on an Async Task running on it's own thread (Network Exception on main thread):

 try {
        try
    {   
          String lat = String.valueOf(latitude);
          String lon = String.valueOf(longitude);
          URL url = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Map/AerialWithLabels/" + lat 
      + "," + lon +"/18?mapSize=480,840&pp="+ lat +"," + lon + ";21;U&key=AsDTsMUE7Rr9oqizyP434eZR9L7UULMuuVZ4Qd-d0K3rqcBz");
   
          switch (API_Mode) {
          default
          url = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Map/AerialWithLabels/" + lat 
      + "," + lon +"/18?mapSize=480,840&pp="+ lat +"," + lon + ";21;U&key=AsDTsMUE7Rr9oqizyPuGA8eGNgCUalK9TOTuVZ4Qd-d0K3rqcBz");
    break;  
          case GOOGLE
          url = new URL("http://maps.googleapis.com/maps/api/staticmap?center="
          lat +"," + lon + "&zoom=18&size=480x840&markers=color:red|color:red|label:U|"
          lat +"," + lon + "&sensor=false&maptype=hybrid");
    break;  
          case MAPQUEST
            url = new URL("http://www.mapquestapi.com/staticmap/v3/getmap?key=Fmjtd|luua29uanl,rw=o5-hwtsd&center="
                lat + ","+ lon +"&zoom=15&size=480,840&type=hyb&imagetype=jpg&pois=U,"
            lat + "," + lon +",-20,-20|mcenter," + lat + "," + lon + "");
          break;  
          }
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);                   
      urlConnection.connect();                  
      File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
      String timeStamp = dateFormat.format(new Date());
      String filename="currentLocation_" + timeStamp + ".jpeg";   
      Log.i("Local filename:",""+filename);
      File file = new File(SDCardRoot,filename);
      if(file.createNewFile())
      {
        file.createNewFile();
      }                 
      FileOutputStream fileOutput = new FileOutputStream(file);
      InputStream inputStream = urlConnection.getInputStream();
      int totalSize = urlConnection.getContentLength();
      int downloadedSize = 0;   
      byte[] buffer = new byte[2048];
      int bufferLength = 0;
      while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
      {                 
        fileOutput.write(buffer, 0, bufferLength);                  
        downloadedSize += bufferLength;                 
        Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
      }             
      fileOutput.close();
      Intent sendIntent = new Intent(Intent.ACTION_SEND); 
      sendIntent.putExtra("address", Number);
            //sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
            sendIntent.putExtra("sms_body", "My Current Location: " + lat + "," + lon); 
            sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/" + filename));
            sendIntent.setType("image/jpg");
            startActivity(sendIntent);
   
    catch (MalformedURLException e) 
    {
      e.printStackTrace();
   
    catch (IOException e)
    {
      e.printStackTrace();
    }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...