diff --git a/app/src/main/java/org/ntlab/amaryllis/client/MainActivity.java b/app/src/main/java/org/ntlab/amaryllis/client/MainActivity.java index 3319aba..dbb8327 100644 --- a/app/src/main/java/org/ntlab/amaryllis/client/MainActivity.java +++ b/app/src/main/java/org/ntlab/amaryllis/client/MainActivity.java @@ -46,18 +46,28 @@ import org.ntlab.amaryllis.client.fragment.MapsFragment; import org.ntlab.amaryllis.client.resources.AccountsRest; import org.ntlab.amaryllis.client.resources.CategoriesRest; +import org.ntlab.amaryllis.client.resources.VoicememosRest; import org.ntlab.amaryllis.client.voiceservice.TestVoiceService; import org.ntlab.amaryllis.client.voiceservice.VoiceMemo; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import okhttp3.OkHttpClient; +import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; +import retrofit2.http.GET; +import retrofit2.http.Url; public class MainActivity extends AppCompatActivity implements LocationListener { @@ -176,7 +186,7 @@ manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - + install("12adfee2-35ad-4c98-85a7-046206420ec2.3gp"); } @@ -285,6 +295,84 @@ return categories; } + public interface IVoice { + // option 2: using a dynamic URL + @GET + Call downloadVoiceFile(@Url String fileUrl); + } + void install(String u){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/data/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + IVoice ivoice = retrofit.create(IVoice.class); + Call call = ivoice.downloadVoiceFile(u); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + boolean writtenToDisk = writeResponseBodyToDisk(response.body()); + + + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + private boolean writeResponseBodyToDisk(ResponseBody body) { + try { + // todo change the file location/name according to your needs + File futureStudioIconFile = new File( this.getExternalFilesDir(null).getAbsolutePath() + "/sample.3gp"); + + InputStream inputStream = null; + OutputStream outputStream = null; + + try { + byte[] fileReader = new byte[409600]; + + long fileSize = body.contentLength(); + long fileSizeDownloaded = 0; + + inputStream = body.byteStream(); + outputStream = new FileOutputStream(futureStudioIconFile); + + while (true) { + int read = inputStream.read(fileReader); + + if (read == -1) { + break; + } + + outputStream.write(fileReader, 0, read); + + fileSizeDownloaded += read; + + Log.d("File Download: " , fileSizeDownloaded + " of " + fileSize); + } + + outputStream.flush(); + + return true; + } catch (IOException e) { + return false; + } finally { + if (inputStream != null) { + inputStream.close(); + } + + if (outputStream != null) { + outputStream.close(); + } + } + } catch (IOException e) { + return false; + } + } + // // @Override // public boolean onCreateOptionsMenu(Menu menu) {