1 package net.osdn.gokigen.gr2control.playback.detail;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
10 import java.util.Locale;
13 import android.app.Activity;
14 import android.app.AlertDialog;
15 import android.app.ProgressDialog;
16 import android.content.ContentResolver;
17 import android.content.ContentValues;
18 import android.content.Intent;
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.Matrix;
23 import android.net.Uri;
24 import android.support.annotation.NonNull;
25 import android.support.media.ExifInterface;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.provider.MediaStore;
29 import android.provider.MediaStore.Images;
30 import android.support.v4.app.Fragment;
31 import android.support.v4.app.FragmentActivity;
32 import android.support.v4.view.PagerAdapter;
33 import android.support.v4.view.ViewPager;
34 import android.support.v4.view.ViewPager.OnPageChangeListener;
35 import android.support.v7.app.ActionBar;
36 import android.support.v7.app.AppCompatActivity;
37 import android.util.Log;
38 import android.util.LruCache;
39 import android.view.LayoutInflater;
40 import android.view.Menu;
41 import android.view.MenuInflater;
42 import android.view.MenuItem;
43 import android.view.View;
44 import android.view.ViewGroup;
45 import android.widget.ImageView;
46 import android.widget.Toast;
48 import net.osdn.gokigen.gr2control.R;
49 import net.osdn.gokigen.gr2control.camera.ICameraFileInfo;
50 import net.osdn.gokigen.gr2control.camera.playback.IDownloadImageCallback;
51 import net.osdn.gokigen.gr2control.camera.playback.IDownloadLargeContentCallback;
52 import net.osdn.gokigen.gr2control.camera.playback.IDownloadThumbnailImageCallback;
53 import net.osdn.gokigen.gr2control.camera.playback.IPlaybackControl;
54 import net.osdn.gokigen.gr2control.camera.playback.ProgressEvent;
56 public class ImagePagerViewFragment extends Fragment
58 private final String TAG = this.toString();
59 private final String JPEG_SUFFIX = ".jpg";
60 private final String RAW_SUFFIX = ".dng";
61 private IPlaybackControl playbackControl;
63 private List<ImageContentInfoEx> contentList = null;
64 private int contentIndex = 0;
66 private LayoutInflater layoutInflater = null;
67 private ViewPager viewPager = null;
68 private LruCache<String, Bitmap> imageCache =null;
70 public static ImagePagerViewFragment newInstance(@NonNull IPlaybackControl playbackControl, @NonNull List<ImageContentInfoEx> contentList, int contentIndex)
72 ImagePagerViewFragment fragment = new ImagePagerViewFragment();
73 fragment.setInterface(playbackControl);
74 fragment.setContentList(contentList, contentIndex);
78 private void setInterface(@NonNull IPlaybackControl playbackControl)
80 this.playbackControl = playbackControl;
83 private void setContentList(@NonNull List<ImageContentInfoEx> contentList, int contentIndex)
85 this.contentList = contentList;
86 this.contentIndex = contentIndex;
90 public void onCreate(Bundle savedInstanceState)
92 super.onCreate(savedInstanceState);
93 imageCache = new LruCache<>(5);
94 setHasOptionsMenu(true);
98 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
100 layoutInflater = inflater;
101 View view = layoutInflater.inflate(R.layout.fragment_image_pager_view, container, false);
102 viewPager = view.findViewById(R.id.viewPager1);
103 viewPager.setAdapter(new ImagePagerAdapter());
104 viewPager.addOnPageChangeListener(new ImagePageChangeListener());
110 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
114 ImageContentInfoEx info = contentList.get(contentIndex);
115 ICameraFileInfo file = info.getFileInfo();
116 String path = file.getDirectoryPath() + "/" + file.getFilename();
118 AppCompatActivity activity = (AppCompatActivity) getActivity();
119 if (activity != null)
121 ActionBar bar = activity.getSupportActionBar();
128 String lowerCasePath = path.toLowerCase();
129 if (lowerCasePath.endsWith(JPEG_SUFFIX))
133 inflater.inflate(R.menu.image_view_with_raw, menu);
134 MenuItem downloadMenuItem = menu.findItem(R.id.action_download_with_raw);
135 downloadMenuItem.setEnabled(true);
139 inflater.inflate(R.menu.image_view, menu);
140 MenuItem downloadMenuItem = menu.findItem(R.id.action_download);
141 downloadMenuItem.setEnabled(true);
146 inflater.inflate(R.menu.movie_view, menu);
147 MenuItem downloadMenuItem = menu.findItem(R.id.action_download_movie);
148 downloadMenuItem.setEnabled(true);
158 public boolean onOptionsItemSelected(MenuItem item)
160 boolean doDownload = false;
161 boolean getInformation = false;
162 boolean isSmallSize = false;
163 String specialSuffix = null;
164 if ((item.getItemId() == R.id.action_get_information)||(item.getItemId() == R.id.action_get_information_raw))
166 getInformation = true;
168 else if ((item.getItemId() == R.id.action_download_original_size)||(item.getItemId() == R.id.action_download_original_size_raw))
172 else if ((item.getItemId() == R.id.action_download_640x480)||(item.getItemId() == R.id.action_download_640x480_raw))
177 else if (item.getItemId() == R.id.action_download_original_movie)
181 else if (item.getItemId() == R.id.action_download_raw)
184 specialSuffix = RAW_SUFFIX;
189 showFileInformation((contentList.get(contentIndex)).getFileInfo());
197 ICameraFileInfo file = (contentList.get(contentIndex)).getFileInfo();
198 String path = file.getDirectoryPath() + "/" + file.getFilename();
199 String lowerCasePath = path.toLowerCase();
200 String suffix = (specialSuffix == null) ? lowerCasePath.substring(lowerCasePath.lastIndexOf(".")) : specialSuffix;
201 Calendar calendar = Calendar.getInstance();
202 String filename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(calendar.getTime()) + suffix;
205 saveImageWithDialog(filename, downloadSize, getInformation);
213 return (super.onOptionsItemSelected(item));
216 private void showFileInformation(final ICameraFileInfo fileInfo)
218 runOnUiThread(new Runnable() {
223 String model = fileInfo.getModel();
224 String dateTime = "";
225 Date date = fileInfo.getDatetime();
227 dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US).format(date);
229 String path = fileInfo.getDirectoryPath() + "/" + fileInfo.getFilename();
230 String shutter = fileInfo.getShutterSpeed();
231 shutter = shutter.replace(".", "/");
232 String aperture = fileInfo.getAperature();
233 String iso = fileInfo.getIsoSensitivity();
234 String expRev = fileInfo.getExpRev();
236 message = path + "\r\n" + dateTime + "\r\n" + "- - - - - - - - - -\r\n " + shutter + " F" + aperture + " (" + expRev + ")" + " ISO" + iso + "\r\n" + "- - - - - - - - - -\r\n" + " model : " + model + "\r\n";
242 presentMessage(getString(R.string.download_control_get_information_title), message);
250 public void onResume()
253 AppCompatActivity activity = (AppCompatActivity)getActivity();
254 if (activity != null)
256 ActionBar bar = activity.getSupportActionBar();
259 bar.setDisplayShowHomeEnabled(true);
263 viewPager.setCurrentItem(contentIndex);
267 public void onPause()
270 AppCompatActivity activity = (AppCompatActivity)getActivity();
271 if (activity != null)
273 ActionBar bar = activity.getSupportActionBar();
281 private class ImagePagerAdapter extends PagerAdapter
285 public int getCount() {
286 return contentList.size();
290 public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
292 return (view.equals(object));
296 public @NonNull Object instantiateItem(@NonNull ViewGroup container, int position)
298 ImageView view = (ImageView)layoutInflater.inflate(R.layout.view_image_page, container, false);
299 container.addView(view);
300 downloadImage(position, view);
305 public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
307 container.removeView((ImageView)object);
312 private class ImagePageChangeListener implements OnPageChangeListener
316 public void onPageScrollStateChanged(int state)
322 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
328 public void onPageSelected(int position)
330 contentIndex = position;
333 ICameraFileInfo file = (contentList.get(contentIndex)).getFileInfo();
334 String path = file.getDirectoryPath() + "/" + file.getFilename();
336 AppCompatActivity activity = (AppCompatActivity) getActivity();
337 if (activity != null)
339 ActionBar bar = activity.getSupportActionBar();
344 activity.getSupportActionBar().setTitle(path);
345 activity.getFragmentManager().invalidateOptionsMenu();
355 private void downloadImage(final int position, final ImageView view)
357 Thread thread = new Thread(new Runnable() {
360 downloadImageImpl(position, view);
373 private void downloadImageImpl(int position, final ImageView view)
377 ICameraFileInfo file = (contentList.get(position)).getFileInfo();
378 final String path = file.getDirectoryPath() + "/" + file.getFilename();
380 // Get the cached image.
381 final Bitmap bitmap = imageCache.get(path);
384 if (view != null && viewPager.indexOfChild(view) > -1)
386 runOnUiThread(new Runnable()
391 view.setImageBitmap(bitmap);
398 // Download the image.
399 playbackControl.downloadContentScreennail(path, new IDownloadThumbnailImageCallback() {
401 public void onProgress(ProgressEvent e) {
402 // MARK: Do not use to cancel a downloading by progress handler.
403 // A communication error may occur by the downloading of the next image when
404 // you cancel the downloading of the image by a progress handler in
405 // the current version.
409 //public void onCompleted(final byte[] data, final Map<String, Object> metadata) {
410 public void onCompleted(final Bitmap bitmap, final Map<String, Object> metadata) {
411 // Cache the downloaded image.
413 //final Bitmap bitmap = createRotatedBitmap(data, metadata);
420 if (imageCache != null)
422 imageCache.put(path, bitmap);
429 runOnUiThread(new Runnable() {
432 if ((bitmap != null) && (view != null) && (viewPager.indexOfChild(view) > -1))
434 view.setImageBitmap(bitmap);
441 public void onErrorOccurred(Exception e) {
442 final String message = e.getMessage();
443 runOnUiThread(new Runnable() {
446 presentMessage("Load failed", message);
459 * デバイスに画像ファイルをダウンロード(保存)する
461 * @param filename ファイル名(カメラ内の)
462 * @param downloadSize ダウンロードサイズ
463 * @param isGetInformationMode 情報取得モードか?
465 public void saveImageWithDialog(final String filename, float downloadSize, boolean isGetInformationMode)
469 if (filename.endsWith(JPEG_SUFFIX)) {
471 MyImageDownloader imageDownloader = new MyImageDownloader(filename, downloadSize, isGetInformationMode);
472 imageDownloader.startDownload();
475 MyMovieDownloader movieDownloader = new MyMovieDownloader(filename);
476 movieDownloader.startDownload();
486 // -------------------------------------------------------------------------
488 // -------------------------------------------------------------------------
490 private void presentMessage(String title, String message)
492 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
493 builder.setTitle(title).setMessage(message);
497 private void runOnUiThread(Runnable action)
499 if (getActivity() == null)
503 getActivity().runOnUiThread(action);
506 private Bitmap createRotatedBitmap(byte[] data, Map<String, Object> metadata)
511 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
513 catch (OutOfMemoryError e)
525 int degrees = getRotationDegrees(data, metadata);
528 Matrix m = new Matrix();
529 m.postRotate(degrees);
532 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
534 catch (OutOfMemoryError e)
544 private int getRotationDegrees(byte[] data, Map<String, Object> metadata)
547 int orientation = ExifInterface.ORIENTATION_UNDEFINED;
549 if (metadata != null && metadata.containsKey("Orientation")) {
550 orientation = Integer.parseInt((String)metadata.get("Orientation"));
554 // Gets image orientation to display a picture.
557 File tempFile = File.createTempFile("temp", null);
559 FileOutputStream outStream = new FileOutputStream(tempFile.getAbsolutePath());
560 outStream.write(data);
563 ExifInterface exifInterface = new ExifInterface(tempFile.getAbsolutePath());
564 orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
565 if (!tempFile.delete())
567 Log.v(TAG, "temp file delete failure.");
570 catch (IOException e)
578 case ExifInterface.ORIENTATION_NORMAL:
581 case ExifInterface.ORIENTATION_ROTATE_90:
584 case ExifInterface.ORIENTATION_ROTATE_180:
587 case ExifInterface.ORIENTATION_ROTATE_270:
597 * 静止画のダウンロード(とEXIF情報の取得)
600 private class MyImageDownloader implements IDownloadImageCallback
602 private boolean isGetInformation;
603 private ProgressDialog downloadDialog = null;
604 private String filename;
605 private float downloadImageSize;
610 * @param filename ファイル名
611 * @param downloadSize ダウンロードのサイズ
612 * @param isGetInformation 情報を取得するだけかどうか(trueなら情報を取得するだけ)
614 MyImageDownloader(final String filename, float downloadSize, boolean isGetInformation)
616 this.filename = filename;
617 this.downloadImageSize = downloadSize;
618 this.isGetInformation = isGetInformation;
627 Log.v(TAG, "startDownload() " + filename);
628 downloadDialog = new ProgressDialog(getContext());
629 if (isGetInformation)
631 downloadDialog.setTitle(getString(R.string.dialog_get_information_title));
635 downloadDialog.setTitle(getString(R.string.dialog_download_title));
637 downloadDialog.setMessage(getString(R.string.dialog_download_message) + " " + filename);
638 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
639 downloadDialog.setCancelable(false);
640 downloadDialog.show();
642 // Download the image.
645 ICameraFileInfo file = (contentList.get(contentIndex)).getFileInfo();
646 String path = file.getDirectoryPath() + "/" + file.getFilename();
647 playbackControl.downloadImage(path, downloadImageSize, this);
658 * @param progressEvent 進捗情報
661 public void onProgress(ProgressEvent progressEvent)
664 if (downloadDialog != null)
666 int percent = (int)(progressEvent.getProgress() * 100.0f);
667 downloadDialog.setProgress(percent);
668 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
675 * @param bytes 受信バイト数
679 public void onCompleted(byte[] bytes, Map<String, Object> map)
682 if (isGetInformation) {
683 // Exif情報をダイアログ表示して終わる
684 showExifInformation(bytes);
689 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + getString(R.string.app_name2) + "/";
690 final String filepath = new File(directoryPath.toLowerCase(), filename).getPath();
694 final File directory = new File(directoryPath);
695 if (!directory.exists()) {
696 if (!directory.mkdirs()) {
697 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
700 FileOutputStream outputStream = new FileOutputStream(filepath);
701 outputStream.write(bytes);
702 outputStream.close();
703 } catch (Exception e) {
704 final String message = e.getMessage();
705 runOnUiThread(new Runnable() {
708 if (downloadDialog != null) {
709 downloadDialog.dismiss();
711 presentMessage(getString(R.string.download_control_save_failed), message);
714 // ダウンロード失敗時には、ギャラリーにデータ登録を行わない。
718 boolean hasGps = false;
719 float[] latLong = new float[2];
723 ExifInterface exif = new ExifInterface(filepath);
724 hasGps = exif.getLatLong(latLong);
731 // ギャラリーに受信したファイルを登録する
734 long now = System.currentTimeMillis();
735 ContentValues values = new ContentValues();
736 values.put(Images.Media.MIME_TYPE, "image/jpeg");
737 values.put(Images.Media.DATA, filepath);
738 values.put(Images.Media.DATE_ADDED, now);
739 values.put(Images.Media.DATE_TAKEN, now);
740 values.put(Images.Media.DATE_MODIFIED, now);
741 values.put(Images.Media.ORIENTATION, getRotationDegrees(bytes, map));
744 values.put(MediaStore.Images.Media.LATITUDE, latLong[0]);
745 values.put(MediaStore.Images.Media.LONGITUDE, latLong[1]);
747 Activity activity = getActivity();
748 if (activity != null)
750 ContentResolver resolver = getActivity().getContentResolver();
751 final Uri insertedImage = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
754 runOnUiThread(new Runnable() {
758 if (downloadDialog != null)
760 downloadDialog.dismiss();
762 Toast.makeText(getActivity(), getString(R.string.download_control_save_success) + " " + filename, Toast.LENGTH_SHORT).show();
766 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
767 if (preferences.getBoolean(ICameraPropertyAccessor.SHARE_AFTER_SAVE, false))
769 shareContent(insertedImage);
779 } catch (Exception e) {
780 final String message = e.getMessage();
781 runOnUiThread(new Runnable() {
784 if (downloadDialog != null)
786 downloadDialog.dismiss();
788 presentMessage(getString(R.string.download_control_save_failed), message);
801 public void onErrorOccurred(Exception e)
803 final String message = e.getMessage();
804 runOnUiThread(new Runnable()
809 if (downloadDialog != null)
811 downloadDialog.dismiss();
813 if (isGetInformation)
815 presentMessage(getString(R.string.download_control_get_information_failed), message);
819 presentMessage(getString(R.string.download_control_download_failed), message);
828 * @param pictureUri 画像ファイル名
830 private void shareContent(final Uri pictureUri)
832 Intent intent = new Intent();
833 intent.setAction(Intent.ACTION_SEND);
836 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
837 intent.setType("image/jpeg");
838 intent.putExtra(Intent.EXTRA_STREAM, pictureUri);
839 getActivity().startActivityForResult(intent, 0);
844 Log.v(TAG, " URI : " + pictureUri);
851 * EXIF情報の表示 (ExifInterface を作って、表示クラスに渡す)
855 private void showExifInformation(byte[] bytes)
857 ExifInterface exif = null;
860 Calendar calendar = Calendar.getInstance();
861 String filename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(calendar.getTime());
862 File tempFile = File.createTempFile(filename, null);
864 FileOutputStream outStream = new FileOutputStream(tempFile.getAbsolutePath());
865 outStream.write(bytes);
868 exif = new ExifInterface(tempFile.getAbsolutePath());
869 if (!tempFile.delete())
871 Log.v(TAG, "temp file delete failure.");
874 catch (IOException e)
878 runOnUiThread(new ExifInfoToShow(exif));
883 * (クラス生成時に、表示情報を作り出す)
885 private class ExifInfoToShow implements Runnable
887 private String message = "";
891 * @param information メッセージ
893 ExifInfoToShow(ExifInterface information)
895 this.message = formMessage(information);
899 * Exif情報を表示に適した形式に変更し、情報を表示する
900 * @param exifInterface Exif情報
901 * @return 表示に適したExif情報
903 private String formMessage(ExifInterface exifInterface)
906 if (exifInterface != null) {
908 msg = msg + getString(R.string.exif_datetime_title);
909 msg = msg + " " + getExifAttribute(exifInterface, ExifInterface.TAG_DATETIME) + "\r\n"; //(string)
910 //msg = msg + "\r\n";
913 double focalLength = exifInterface.getAttributeDouble(ExifInterface.TAG_FOCAL_LENGTH, 0.0f);
914 msg = msg + getString(R.string.exif_focal_length_title);
915 msg = msg + " " + String.valueOf(focalLength) + "mm ";
916 msg = msg + "(" + getString(R.string.exif_focal_35mm_equiv_title) + " " + String.valueOf(focalLength * 2.0d) + "mm)" + "\r\n";
919 // カスタムイメージプロセッシング利用の有無
920 //if (exifInterface.getAttributeInt(ExifInterface.TAG_CUSTOM_RENDERED, 0) != 0)
922 // msg = msg + getString(R.string.exif_custom_process_title) + "\r\n";
926 String[] stringArray = getResources().getStringArray(R.array.exif_exposure_program_value);
927 int exposureProgram = exifInterface.getAttributeInt(ExifInterface.TAG_EXPOSURE_PROGRAM, 0);
928 msg = msg + getString(R.string.exif_camera_mode_title);
929 msg = msg + " " + ((stringArray.length > exposureProgram) ? stringArray[exposureProgram] : ("? (" + exposureProgram + ")")) + "\r\n";
930 //msg = msg + "\r\n";
933 String[] meteringStringArray = getResources().getStringArray(R.array.exif_metering_mode_value);
934 int metering = exifInterface.getAttributeInt(ExifInterface.TAG_METERING_MODE, 0);
935 msg = msg + getString(R.string.exif_metering_mode_title);
936 msg = msg + " " + ((meteringStringArray.length > metering) ? meteringStringArray[metering] : ("? (" + metering + ")")) + "\r\n";
937 //msg = msg + "\r\n";
940 msg = msg + getString(R.string.exif_exposure_time_title);
941 String expTime = getExifAttribute(exifInterface, ExifInterface.TAG_EXPOSURE_TIME);
942 float val, inv = 0.0f;
945 val = Float.parseFloat(expTime);
950 if (inv < 2.0f) // if (inv < 10.0f)
961 //msg = msg + " " + expTime + "s "; //(string)
965 int intValue = (int) inv;
966 int modValue = intValue % 10;
967 if ((modValue == 9)||(modValue == 4))
969 // ちょっと格好が悪いけど...切り上げ
972 msg = msg + " 1/" + intValue + " s ";
976 // シャッター速度を数値(秒数)で表示する
977 msg = msg + " " + expTime + "s "; //(string)
982 msg = msg + getString(R.string.exif_aperture_title);
983 msg = msg + " " + getExifAttribute(exifInterface, ExifInterface.TAG_F_NUMBER) + "\r\n"; // (string)
986 msg = msg + getString(R.string.exif_iso_title);
987 msg = msg + " " + getExifAttribute(exifInterface, ExifInterface.TAG_ISO_SPEED_RATINGS) + "\r\n"; // (string)
992 msg = msg + getString(R.string.exif_maker_title);
993 msg = msg + " " + getExifAttribute(exifInterface, ExifInterface.TAG_MAKE) + "\r\n";
996 msg = msg + getString(R.string.exif_camera_title);
997 msg = msg + " " + getExifAttribute(exifInterface, ExifInterface.TAG_MODEL)+ "\r\n"; // (string)
999 String lat = getExifAttribute(exifInterface, ExifInterface.TAG_GPS_LATITUDE);
1000 if ((lat != null)&&(lat.length() > 0))
1003 msg = msg + "\r\n " + getString(R.string.exif_with_gps) + "\r\n";
1005 //msg = msg + getExifAttribute(exifInterface, ExifInterface.TAG_FLASH); // フラッシュ (int)
1006 //msg = msg + getExifAttribute(exifInterface, ExifInterface.TAG_ORIENTATION); // 画像の向き (int)
1007 //msg = msg + getExifAttribute(exifInterface, ExifInterface.TAG_WHITE_BALANCE); // ホワイトバランス (int)
1009 // その他の情報...EXIFタグで取得できたものをログにダンプする
1010 msg = msg + ExifInformationDumper.dumpExifInformation(exifInterface, false);
1014 msg = getString(R.string.download_control_get_information_failed);
1019 private String getExifAttribute(ExifInterface attr, String tag)
1021 String value = attr.getAttribute(tag);
1033 if (downloadDialog != null)
1035 downloadDialog.dismiss();
1037 presentMessage(getString(R.string.download_control_get_information_title), message);
1044 * 動画(とRAWファイル)のダウンロード
1047 private class MyMovieDownloader implements IDownloadLargeContentCallback
1049 private ProgressDialog downloadDialog = null;
1050 private String filename = null;
1051 private String filepath = null;
1052 private FileOutputStream outputStream = null;
1057 * @param filename ファイル名
1059 MyMovieDownloader(final String filename)
1061 this.filename = filename;
1068 void startDownload()
1070 Log.v(TAG, "startDownload() " + filename);
1071 downloadDialog = new ProgressDialog(getContext());
1072 downloadDialog.setTitle(getString(R.string.dialog_download_file_title));
1073 downloadDialog.setMessage(getString(R.string.dialog_download_message) + " " + filename);
1074 downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
1075 downloadDialog.setCancelable(false);
1076 downloadDialog.show();
1078 // Download the image.
1081 ImageContentInfoEx content = contentList.get(contentIndex);
1082 ICameraFileInfo file = content.getFileInfo();
1083 String targetFileName = file.getFilename();
1084 if (content.hasRaw())
1086 targetFileName = targetFileName.replace(".JPG", ".ORF");
1088 String path = file.getDirectoryPath() + "/" + targetFileName;
1089 Log.v(TAG, "downloadLargeContent : " + path);
1090 playbackControl.downloadLargeContent(path, this);
1092 final String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + getString(R.string.app_name2) + "/";
1093 filepath = new File(directoryPath.toLowerCase(), filename).getPath();
1095 final File directory = new File(directoryPath);
1096 if (!directory.exists())
1098 if (!directory.mkdirs())
1100 Log.v(TAG, "MKDIR FAIL. : " + directoryPath);
1103 outputStream = new FileOutputStream(filepath);
1107 final String message = e.getMessage();
1108 runOnUiThread(new Runnable() {
1111 if (downloadDialog != null) {
1112 downloadDialog.dismiss();
1114 presentMessage(getString(R.string.download_control_save_failed), message);
1119 catch (Exception ex)
1121 ex.printStackTrace();
1126 public void onProgress(byte[] bytes, ProgressEvent progressEvent)
1128 if (downloadDialog != null)
1130 int percent = (int)(progressEvent.getProgress() * 100.0f);
1131 downloadDialog.setProgress(percent);
1132 //downloadDialog.setCancelable(progressEvent.isCancellable()); // キャンセルできるようにしないほうが良さそうなので
1136 if (outputStream != null)
1138 outputStream.write(bytes);
1143 e.printStackTrace();
1148 public void onCompleted()
1152 if (outputStream != null)
1154 outputStream.flush();
1155 outputStream.close();
1156 outputStream = null;
1158 if (!filename.endsWith(RAW_SUFFIX))
1160 // ギャラリーに受信したファイルを登録する
1161 long now = System.currentTimeMillis();
1162 ContentValues values = new ContentValues();
1163 ContentResolver resolver = getActivity().getContentResolver();
1164 values.put(Images.Media.MIME_TYPE, "video/mp4");
1165 values.put(Images.Media.DATA, filepath);
1166 values.put(Images.Media.DATE_ADDED, now);
1167 values.put(Images.Media.DATE_TAKEN, now);
1168 values.put(Images.Media.DATE_MODIFIED, now);
1169 final Uri content = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
1173 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
1174 if (preferences.getBoolean(ICameraPropertyAccessor.SHARE_AFTER_SAVE, false))
1176 runOnUiThread(new Runnable()
1181 shareContent(content);
1188 e.printStackTrace();
1192 runOnUiThread(new Runnable() {
1196 if (downloadDialog != null)
1198 downloadDialog.dismiss();
1200 Toast.makeText(getActivity(), getString(R.string.download_control_save_success) + " " + filename, Toast.LENGTH_SHORT).show();
1207 final String message = e.getMessage();
1208 runOnUiThread(new Runnable() {
1211 if (downloadDialog != null)
1213 downloadDialog.dismiss();
1215 presentMessage(getString(R.string.download_control_save_failed), message);
1223 public void onErrorOccurred(Exception e)
1225 final String message = e.getMessage();
1228 if (outputStream != null)
1230 outputStream.flush();
1231 outputStream.close();
1232 outputStream = null;
1235 catch (Exception ex)
1237 e.printStackTrace();
1238 ex.printStackTrace();
1240 runOnUiThread(new Runnable()
1245 if (downloadDialog != null)
1247 downloadDialog.dismiss();
1249 presentMessage(getString(R.string.download_control_download_failed), message);
1259 * @param movieFileUri 動画ファイルUri
1261 private void shareContent(final Uri movieFileUri)
1263 Intent intent = new Intent();
1264 intent.setAction(Intent.ACTION_SEND);
1267 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1268 intent.setType("video/mp4");
1269 intent.putExtra(Intent.EXTRA_STREAM, movieFileUri);
1270 FragmentActivity activity = getActivity();
1271 if (activity != null)
1273 activity.startActivityForResult(intent, 0);
1278 e.printStackTrace();