OSDN Git Service

背景画像の表示機能を復活させる。
authorMRSa <mrsa@myad.jp>
Thu, 19 Jul 2018 15:46:25 +0000 (00:46 +0900)
committerMRSa <mrsa@myad.jp>
Thu, 19 Jul 2018 15:46:25 +0000 (00:46 +0900)
app/src/main/java/jp/sourceforge/gokigen/memoma/MeMoMaListener.java
app/src/main/java/jp/sourceforge/gokigen/memoma/drawers/MeMoMaCanvasDrawer.java
app/src/main/java/jp/sourceforge/gokigen/memoma/fileio/ImageLoader.java

index ccbf399..3b01b87 100644 (file)
@@ -10,6 +10,7 @@ import android.graphics.Color;
 import android.graphics.PorterDuff.Mode;
 import android.graphics.drawable.BitmapDrawable;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.v7.app.AppCompatActivity;
@@ -225,9 +226,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
            
            /**
             *  他画面から戻ってきたとき...
-            * @param requestCode
-            * @param resultCode
-            * @param data
+                *
             */
            public void onActivityResult(int requestCode, int resultCode, Intent data)
            {
@@ -238,13 +237,25 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
                        // 取得したuri を preferenceに記録する
                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(parent);
                        Uri backgroundUri = data.getData();
-                       SharedPreferences.Editor editor = preferences.edit();
-                       editor.putString("backgroundUri", backgroundUri.toString());
-                       editor.commit();
-                       
-                       // 背景画像イメージの更新処理
-                       updateBackgroundImage(backgroundUri.toString());
-                       
+                       if (backgroundUri != null)
+                       {
+                           try
+                        {
+                            if (Build.VERSION.SDK_INT >= 19) {
+                                final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION);
+                                parent.getContentResolver().takePersistableUriPermission(backgroundUri, takeFlags);
+                            }
+                            SharedPreferences.Editor editor = preferences.edit();
+                            editor.putString("backgroundUri", backgroundUri.toString());
+                            editor.apply();
+                        }
+                        catch (Exception e)
+                        {
+                            e.printStackTrace();
+                        }
+                                               // 背景画像イメージの更新処理
+                                               updateBackgroundImage(backgroundUri.toString());
+                                       }
                            System.gc();
                    }
                    catch (Exception ex)
@@ -514,7 +525,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
                        break;
 
                  case android.R.id.home:
-                       /** アイコンが押された時の処理... **/
+                       // アイコンが押された時の処理...
                        // テキスト編集ダイアログを表示する
                    showInfoMessageEditDialog();
                            result = true;
@@ -533,16 +544,24 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
             */
            private void insertPicture()
            {
-               Intent intent = new Intent();
-               intent.setType("image/*");
-               intent.setAction(Intent.ACTION_GET_CONTENT);
+               Intent intent;
+            if (Build.VERSION.SDK_INT >= 19) {
+                intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
+                intent.addCategory(Intent.CATEGORY_OPENABLE);
+                intent.setType("image/*");
+                //intent.setAction(Intent.ACTION_GET_CONTENT);
+            } else {
+                intent = new Intent();
+                intent.setType("image/*");
+                intent.setAction(Intent.ACTION_GET_CONTENT);
+            }
                parent.startActivityForResult(intent, MENU_ID_INSERT_PICTURE);
            }
 
            /**
             *    画面キャプチャの実施
             * 
-            * @param isShare
+            *
             */
            private void doCapture(boolean isShare)
            {
@@ -692,7 +711,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
            /**
             *   アイテム選択ダイアログの表示を準備する
             * 
-            * @param dialog
+            *
             */
            private void prepareItemSelectionDialog(Dialog dialog)
            {
@@ -726,6 +745,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
                catch (Exception e)
                {
                     // 例外発生...なにもしない。
+                               e.printStackTrace();
                         //updater.showMessage("ERROR", MainUpdater.SHOWMETHOD_DONTCARE);
                }
            }
@@ -778,7 +798,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
            /**
             *    オブジェクトを本当に削除して良いか確認した後に、オブジェクトを削除する。
             * 
-            * @param key
+            *
             */
            private void removeObject(Integer key)
            {
@@ -830,14 +850,12 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
                 // ダイアログを表示する
                 AlertDialog alertDialog = alertDialogBuilder.create();
                 alertDialog.show();
-
-                return;
            }
 
            /**
             *    オブジェクトを複製する
             * 
-            * @param key
+            *
             */
            private void duplicateObject(Integer key)
            {
@@ -851,7 +869,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
            /**
             *    オブジェクトを拡大する
             * 
-            * @param key
+            *
             */
            private void expandObject(Integer key)
            {
@@ -864,7 +882,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
            /**
             *    オブジェクトを縮小する
             * 
-            * @param key
+            *
             */
            private void shrinkObject(Integer key)
            {
@@ -880,7 +898,7 @@ public class MeMoMaListener implements OnClickListener, OnTouchListener, OnKeyLi
                try
                {
                      BitmapDrawable btnBackgroundShape = (BitmapDrawable)button.getBackground();
-                     if (isHighlight == true)
+                     if (isHighlight)
                      {
 //                             btnBackgroundShape.setColorFilter(Color.rgb(51, 181, 229), Mode.LIGHTEN);
                        btnBackgroundShape.setColorFilter(Color.BLUE, Mode.LIGHTEN);
index bd033bf..e586552 100644 (file)
@@ -137,7 +137,7 @@ public class MeMoMaCanvasDrawer implements ICanvasDrawer,  GestureDetector.OnGes
 
          /**
           * 
-          * @param uri
+          *
           */
          public void updateBackgroundBitmap(String uri, int width, int height)
          {
@@ -147,7 +147,7 @@ public class MeMoMaCanvasDrawer implements ICanvasDrawer,  GestureDetector.OnGes
                  // とりあえず、背景画像をクリアしてガベコレする。
                  backgroundBitmap = null;
                  System.gc();
-                 if (uri.isEmpty() == true)
+                 if (uri.isEmpty())
                  {
                          // 背景画像の指定がなかったので、ここでリターンする。
                          return;
@@ -167,8 +167,7 @@ public class MeMoMaCanvasDrawer implements ICanvasDrawer,  GestureDetector.OnGes
                          backgroundBitmap = null;
                          backgroundBitmapUri = "";
                          System.gc();
-                 }     
-                 return;
+                 }
          }       
          
          /**
index 2f43cb8..d912216 100644 (file)
@@ -4,9 +4,11 @@ import java.io.File;
 import java.io.InputStream;
 import android.app.ProgressDialog;
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Handler;
 import android.os.Message;
 import android.util.Log;
@@ -23,8 +25,8 @@ import jp.sourceforge.gokigen.memoma.R;
  */
 public class ImageLoader
 {
-    ProgressDialog loadingDialog = null;
-    Context  parent = null;
+    ProgressDialog loadingDialog;
+    Context  parent;
 
     // 画像を表示する
     String imageFile = null;
@@ -71,17 +73,14 @@ public class ImageLoader
 
     /**
         *  URIから変換
-        * 
-        * @param imageFile
-        * @return
+        *
         */
        public static Uri parseUri(String imageFile)
        {
-           if (imageFile.startsWith("content://") == true)
+           if (imageFile.startsWith("content://"))
            {
                return (Uri.parse(imageFile));
            }
-
        File picFile = new File(imageFile);
        return (Uri.fromFile(picFile)); 
        }
@@ -93,10 +92,10 @@ public class ImageLoader
        public static void setImage(Context context, ImageView view, String imageFile)
     {
        // 画像を表示する
-               Bitmap bitmap = null;
+               Bitmap bitmap;
                int width = view.getWidth();
                int height = view.getHeight();
-        if (imageFile.startsWith("content://") == true)
+        if (imageFile.startsWith("content://"))
         {
                // URIから画像を設定する...OutOfMemory対策付き
                bitmap = getBitmapFromUri(context, Uri.parse(imageFile), width, height);
@@ -129,18 +128,15 @@ public class ImageLoader
         imageBitmap = null;
         imageWidth = view.getWidth();
         imageHeight = view.getHeight();        
-        
-        /**
-         *  ダイアログ表示中の処理
-         * 
-         */
+
+        // ダイアログ表示中の処理
         Thread thread = new Thread(new Runnable()
         {  
             public void run()
             {
                try
                {                       
-                       if (imageFile.startsWith("content://") == true)
+                       if (imageFile.startsWith("content://"))
                     {
                        // URIから画像を設定する...OutOfMemory対策付き
                        imageBitmap = getBitmapFromUri(parent, Uri.parse(imageFile), imageWidth, imageHeight);
@@ -155,6 +151,7 @@ public class ImageLoader
                 catch (Exception ex)
                {
                        handler.sendEmptyMessage(0);
+                    ex.printStackTrace();
                }
             }
 
@@ -178,7 +175,6 @@ public class ImageLoader
                     imageHeight = 1;
 
                        loadingDialog.dismiss();
-                
                 }
             };   
         });
@@ -188,18 +184,13 @@ public class ImageLoader
         }
         catch (Exception ex)
         {
-
+            ex.printStackTrace();
         }
     }
        
        /**
         *   URI経由でビットマップデータを取得する
         * 
-        * @param context
-        * @param uri
-        * @param width
-        * @param height
-        * @return
         */
     public static Bitmap getBitmapFromUri(Context context, Uri uri, int width, int height)
     {
@@ -214,14 +205,22 @@ public class ImageLoader
 
         InputStream input = null; 
         try
-        { 
-            input = context.getContentResolver().openInputStream(uri); 
-            BitmapFactory.decodeStream(input, null, opt); 
-            input.close();
+        {
+            if (Build.VERSION.SDK_INT >= 19)
+            {
+                context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
+            }
+            input = context.getContentResolver().openInputStream(uri);
+            if (input != null)
+            {
+                BitmapFactory.decodeStream(input, null, opt);
+                input.close();
+            }
         }
         catch (Exception ex)
         {
-               Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString());
+               Log.v(Main.APP_IDENTIFIER, "Ex(1): " + ex.toString() + " URI : " + uri);
+               ex.printStackTrace();
                if (input != null)
                {
                        try
@@ -231,6 +230,7 @@ public class ImageLoader
                        catch (Exception e)
                        {
                                //
+                    e.printStackTrace();
                        }
                }
         }
@@ -272,18 +272,17 @@ public class ImageLoader
                        catch (Exception e)
                        {
                                //
+                    e.printStackTrace();
                        }
                }
+               ex.printStackTrace();
         }
         return (retBitmap);
     }        
     
     /**
      *   ビットマップデータを取得する
-     * 
-     * @param pictureString
-     * @param width
-     * @param height
+     *
      * @return ビットマップデータ
      */
     public static Bitmap getBitmap(String pictureString, int width, int height)