Forráskód Böngészése

[이벤트][Bug] 구글 드라이브 관련 파일의 path 를 가져오지 못해서 발생한 에러 수정

hyodong.min 6 éve
szülő
commit
e4a1687f1d

+ 50 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/util/FileUtil.java

@@ -12,8 +12,12 @@ import android.os.Build;
 import android.os.Environment;
 import android.provider.DocumentsContract;
 import android.provider.MediaStore;
+import android.provider.OpenableColumns;
 import android.util.Log;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.net.URISyntaxException;
 
 /**
@@ -70,6 +74,9 @@ public class FileUtil {
           split[1]
         };
       }
+      else if (isGoogleDriveUri(uri)) {
+        return getDriveFilePath(uri, context);
+      }
     }
 
     if ("content".equalsIgnoreCase(uri.getScheme())) {
@@ -98,6 +105,45 @@ public class FileUtil {
     return null;
   }
 
+  private static String getDriveFilePath(Uri uri, Context context) {
+    Uri returnUri = uri;
+    Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
+    /*
+     * Get the column indexes of the data in the Cursor,
+     *     * move to the first row in the Cursor, get the data,
+     *     * and display it.
+     * */
+    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
+    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
+    returnCursor.moveToFirst();
+    String name = (returnCursor.getString(nameIndex));
+    String size = (Long.toString(returnCursor.getLong(sizeIndex)));
+    File file = new File(context.getCacheDir(), name);
+    try {
+      InputStream inputStream = context.getContentResolver().openInputStream(uri);
+      FileOutputStream outputStream = new FileOutputStream(file);
+      int read = 0;
+      int maxBufferSize = 1 * 1024 * 1024;
+      int bytesAvailable = inputStream.available();
+
+      //int bufferSize = 1024;
+      int bufferSize = Math.min(bytesAvailable, maxBufferSize);
+
+      final byte[] buffers = new byte[bufferSize];
+      while ((read = inputStream.read(buffers)) != -1) {
+        outputStream.write(buffers, 0, read);
+      }
+      Log.e("File Size", "Size " + file.length());
+      inputStream.close();
+      outputStream.close();
+      Log.e("File Path", "Path " + file.getPath());
+      Log.e("File Size", "Size " + file.length());
+    } catch (Exception e) {
+      Log.e("Exception", e.getMessage());
+    }
+    return file.getPath();
+  }
+
   private static boolean isExternalStorageDocument(Uri uri) {
     return "com.android.externalstorage.documents".equals(uri.getAuthority());
   }
@@ -114,4 +160,8 @@ public class FileUtil {
     return "com.google.android.apps.photos.content".equals(uri.getAuthority());
   }
 
+  private static boolean isGoogleDriveUri(Uri uri) {
+    return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
+  }
+
 }

+ 48 - 18
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/event/entry/EventQuestionFragment.java

@@ -1,12 +1,14 @@
 package kr.co.zumo.app.lifeplus.view.screen.event.entry;
 
 import android.Manifest;
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.provider.Settings;
 import android.support.annotation.NonNull;
@@ -53,6 +55,8 @@ import kr.co.zumo.app.lifeplus.view.screen.event.IEventQuestionView;
 public class EventQuestionFragment extends FragmentBase<EventQuestionPresenter> implements IEventQuestionView {
 
   private static final int GET_ALBUM_PHOTO = 1;
+  private static final int GALLERY_INTENT_CALLED = 2;
+  private static final int GALLERY_KITKAT_INTENT_CALLED = 3;
   private LinearLayout layoutContainer;
   private LayoutInflater inflater;
   private View layoutSubmit;
@@ -235,7 +239,7 @@ public class EventQuestionFragment extends FragmentBase<EventQuestionPresenter>
     //공지사항 영역
     View noticeView = inflater.inflate(R.layout.event_participation_item_notice, null);
     String notice = detailBean.getNotice();
-    
+
     if (notice != null && notice.trim().length() != 0) {
       EventQuestionNoticeView eventQuestionNoticeView = new EventQuestionNoticeView(noticeView);
       eventQuestionNoticeView.draw(notice);
@@ -322,27 +326,53 @@ public class EventQuestionFragment extends FragmentBase<EventQuestionPresenter>
    */
   @Override
   public void requestAlbumImage() {
-    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
-    intent.addCategory(Intent.CATEGORY_OPENABLE);
-    intent.setType("image/*");
-    startActivityForResult(intent, GET_ALBUM_PHOTO);
+//    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
+//    intent.addCategory(Intent.CATEGORY_OPENABLE);
+//    intent.setType("image/*");
+//    startActivityForResult(intent, GET_ALBUM_PHOTO);
+
+    if (Build.VERSION.SDK_INT < 19) {
+      Intent intent = new Intent();
+      intent.setType("image/jpeg");
+      intent.setAction(Intent.ACTION_GET_CONTENT);
+      startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.empty_string)), GALLERY_INTENT_CALLED);
+    }
+    else {
+      Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
+      intent.addCategory(Intent.CATEGORY_OPENABLE);
+      intent.setType("image/jpeg");
+      startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
+    }
   }
 
   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
-    if (requestCode == GET_ALBUM_PHOTO) {
-      try {
-        Uri uri = data.getData();
-
-        InputStream inputStream = getContext().getContentResolver().openInputStream(data.getData());
-        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
-        inputStream.close();
-
-        presenter.onImageReceived(uri, bitmap);
-      } catch (Exception e) {
-        //e.printStackTrace();
-        Log.e("APP#  EventQuestionFragment | onActivityResult", "Log.getStackTraceString(e) |" + Log.getStackTraceString(e));
-      }
+    if (resultCode != Activity.RESULT_OK) {
+      return;
+    }
+    if (null == data) {
+      return;
+    }
+
+    Uri uri = data.getData();
+    if (requestCode == GALLERY_INTENT_CALLED) {
+      {} // nothing
+    }
+    else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
+      final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+      // Check for the freshest data.
+      getContext().getContentResolver().takePersistableUriPermission(uri, takeFlags);
+    }
+
+    try {
+      InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
+      Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
+      inputStream.close();
+
+      presenter.onImageReceived(uri, bitmap);
+    } catch (Exception e) {
+      //e.printStackTrace();
+      Log.e("APP#  EventQuestionFragment | onActivityResult", "Log.getStackTraceString(e) |" + Log.getStackTraceString(e));
     }
   }