OSDN Git Service

ファイルのダウンロードが成功するところまで作りこみ。
authorMRSa <mrsa@myad.jp>
Mon, 6 Aug 2018 15:26:30 +0000 (00:26 +0900)
committerMRSa <mrsa@myad.jp>
Mon, 6 Aug 2018 15:26:30 +0000 (00:26 +0900)
app/src/main/java/net/osdn/gokigen/gr2control/camera/playback/IDownloadContentCallback.java
app/src/main/java/net/osdn/gokigen/gr2control/camera/ricohgr2/wrapper/RicohGr2PlaybackControl.java
app/src/main/java/net/osdn/gokigen/gr2control/camera/utils/SimpleHttpClient.java
app/src/main/java/net/osdn/gokigen/gr2control/playback/detail/MyContentDownloader.java

index 4742349..a57ed39 100644 (file)
@@ -4,5 +4,5 @@ public interface IDownloadContentCallback
 {
     void onCompleted();
     void onErrorOccurred(Exception  e);
-    void onProgress(byte[] data, ProgressEvent e);
+    void onProgress(byte[] data, int length, ProgressEvent e);
 }
index 6b45f4c..efb79f7 100644 (file)
@@ -235,10 +235,10 @@ public class RicohGr2PlaybackControl implements IPlaybackControl
                 }
 
                 @Override
-                public void onReceive(int readBytes, int length, byte[] data) {
+                public void onReceive(int readBytes, int length, int size, byte[] data) {
                     float percent = (length == 0) ? 0.0f : ((float) readBytes / (float) length);
                     ProgressEvent event = new ProgressEvent(percent, null);
-                    callback.onProgress(data, event);
+                    callback.onProgress(data, size, event);
                 }
             });
         }
index 56df0c6..24437fa 100644 (file)
@@ -180,17 +180,16 @@ public class SimpleHttpClient
         {
             int contentLength = httpConn.getContentLength();
             byte[] buffer = new byte[BUFFER_SIZE];
-            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
             int readBytes = 0;
-            int readSize = bufferedInputStream.read(buffer, 0, BUFFER_SIZE);
+            int readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
             while (readSize != -1)
             {
-                callback.onReceive(readBytes, contentLength, buffer);
+                callback.onReceive(readBytes, contentLength, readSize, buffer);
                 readBytes += readSize;
-                readSize = bufferedInputStream.read(buffer, 0, BUFFER_SIZE);
+                readSize = inputStream.read(buffer, 0, BUFFER_SIZE);
             }
             Log.v(TAG, "RECEIVED " + readBytes + " BYTES. (contentLength : " + contentLength + ")");
-            bufferedInputStream.close();
+            inputStream.close();
         }
         catch (Exception e)
         {
@@ -393,6 +392,6 @@ public class SimpleHttpClient
     {
         void onCompleted();
         void onErrorOccurred(Exception  e);
-        void onReceive(int readBytes, int length, byte[] data);
+        void onReceive(int readBytes, int length, int size, byte[] data);
     }
 }
index 00cd080..8936080 100644 (file)
@@ -24,6 +24,9 @@ import net.osdn.gokigen.gr2control.preference.IPreferencePropertyAccessor;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Locale;
 
 /**
  *   コンテントのダウンロード
@@ -69,38 +72,39 @@ class MyContentDownloader implements IDownloadContentCallback
         // Download the image.
         try
         {
-            ////// ダイアログの表示
-            activity.runOnUiThread(new Runnable() {
-                @Override
-                public void run() {
-                    downloadDialog = new ProgressDialog(activity);
-                    downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title));
-                    downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + fileInfo.getFilename());
-                    downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-                    downloadDialog.setCancelable(false);
-                    downloadDialog.show();
-                }
-            });
-
+            Calendar calendar = Calendar.getInstance();
+            String extendName = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.getTime());
             targetFileName = fileInfo.getFilename().toUpperCase();
             if (replaceJpegSuffix != null)
             {
                 targetFileName = targetFileName.replace(JPEG_SUFFIX, replaceJpegSuffix);
             }
-            if (targetFileName.contains(RAW_SUFFIX))
+            if (targetFileName.toUpperCase().contains(RAW_SUFFIX))
             {
                 mimeType = "image/x-adobe-dng";
             }
-            else if (targetFileName.contains(MOVIE_SUFFIX))
+            else if (targetFileName.toUpperCase().contains(MOVIE_SUFFIX))
             {
                 mimeType =  "video/mp4";
             }
 
+            ////// ダイアログの表示
+            activity.runOnUiThread(new Runnable() {
+                @Override
+                public void run() {
+                    downloadDialog = new ProgressDialog(activity);
+                    downloadDialog.setTitle(activity.getString(R.string.dialog_download_file_title));
+                    downloadDialog.setMessage(activity.getString(R.string.dialog_download_message) + " " + targetFileName);
+                    downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+                    downloadDialog.setCancelable(false);
+                    downloadDialog.show();
+                }
+            });
             String path = fileInfo.getDirectoryPath() + "/" + targetFileName;
 
-
             final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + activity.getString(R.string.app_name2) + "/";
-            filepath = new File(directoryPath.toLowerCase(), targetFileName.toLowerCase()).getPath();
+            String outputFileName =  extendName + "_" + targetFileName;
+            filepath = new File(directoryPath.toLowerCase(), outputFileName.toLowerCase()).getPath();
             try
             {
                 final File directory = new File(directoryPath);
@@ -136,19 +140,20 @@ class MyContentDownloader implements IDownloadContentCallback
     }
 
     @Override
-    public void onProgress(byte[] bytes, ProgressEvent progressEvent)
+    public void onProgress(byte[] bytes, int length, ProgressEvent progressEvent)
     {
         if (downloadDialog != null)
         {
             int percent = (int)(progressEvent.getProgress() * 100.0f);
             downloadDialog.setProgress(percent);
             //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
+            //Log.v(TAG, "DOWNLOAD (" + percent + "%) " + bytes.length);
         }
         try
         {
             if (outputStream != null)
             {
-                outputStream.write(bytes);
+                outputStream.write(bytes, 0, length);
             }
         }
         catch (Exception e)
@@ -168,7 +173,7 @@ class MyContentDownloader implements IDownloadContentCallback
                 outputStream.close();
                 outputStream = null;
             }
-            if (!targetFileName.endsWith(RAW_SUFFIX))
+            if (!targetFileName.toUpperCase().endsWith(RAW_SUFFIX))
             {
                 // ギャラリーに受信したファイルを登録する
                 long now = System.currentTimeMillis();
@@ -179,26 +184,28 @@ class MyContentDownloader implements IDownloadContentCallback
                 values.put(MediaStore.Images.Media.DATE_ADDED, now);
                 values.put(MediaStore.Images.Media.DATE_TAKEN, now);
                 values.put(MediaStore.Images.Media.DATE_MODIFIED, now);
-                final Uri content = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
-                    try
+                Uri mediaValue = mimeType.contains("video") ? MediaStore.Video.Media.EXTERNAL_CONTENT_URI : MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+                final Uri content = resolver.insert(mediaValue, values);
+                try
+                {
+                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
+                    if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
                     {
-                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
-                        if (preferences.getBoolean(IPreferencePropertyAccessor.SHARE_AFTER_SAVE, false))
+                        activity.runOnUiThread(new Runnable()
                         {
-                            activity.runOnUiThread(new Runnable()
+                            @Override
+                            public void run()
                             {
-                                @Override
-                                public void run()
-                                {
-                                    shareContent(content, mimeType);
-                                }
-                            });
-                        }
-                    }
-                    catch (Exception e)
-                    {
-                        e.printStackTrace();
+                                shareContent(content, mimeType);
+                            }
+                        });
                     }
+                }
+                catch (Exception e)
+                {
+                    e.printStackTrace();
+                }
+
             }
             activity.runOnUiThread(new Runnable() {
                 @Override