在Android 4.4.2版中使用GoogleDrive时无法获取文件路径

时间:2015-05-06 09:38:04

标签: android image file

在我的Android应用程序中,我使用谷歌驱动器选择图像和文件到我的应用程序,它在除4.4.2之外的所有API版本中都能很好地工作,每当我试图选择图像或文件时我都可以获得文件名但不是能够获取文件路径,它总是返回空路径

我的代码:

   // Get real path from Google Drive
        public String getPathfromGoogleDrive(Intent data, Uri contentURI) {

            if (contentURI == null) {
                return null;
            }
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            String mCurrentPhotoPath = new String();

            Cursor cursor = null;

            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            LogUtil.d("currentapiVersion" + currentapiVersion);

            if (currentapiVersion == 19) {

                String wholeID = DocumentsContract.getDocumentId(contentURI);

                // Split at colon, use second item in the array
                String id = wholeID.split(";")[0];

                // where id is equal to
                String sel = MediaStore.Images.Media._ID + "=?";

                cursor = getActivity().getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        filePathColumn, sel, new String[] { id }, null);

                LogUtil.d("Cursor Count" + cursor.getCount());

                if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    mCurrentPhotoPath = cursor.getString(columnIndex);
                    cursor.close();
                }

            }

我的意图:

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;

        if (currentapiVersion == 19) {

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            String strType = "*/*";
            intent.setDataAndType(null, strType);
            startActivityForResult(intent, Gallery);

        } else {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setPackage("com.google.android.apps.docs");
            String strType = "*/*";
            intent.setDataAndType(null, strType);
            startActivityForResult(intent, Gallery);
        }

如果我有任何错误,请纠正我 提前致谢

1 个答案:

答案 0 :(得分:2)

我们可以使用输入流,而不是获取文件真实路径,如下所示

  Bitmap bitmap = null;
    InputStream input = null;
        try {
            input =  getActivity().getContentResolver().openInputStream(selectedImageURI);
        bitmap = BitmapFactory.decodeStream(input);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
           }

从驱动器获取文件并将其写入区域设置(SD卡)

sourceuri - your cnontent uri
destination - path where you want to save in sd card
public boolean savefile(String name, Uri sourceuri, String destination)
            throws IOException {
        // String sourceFilename = sourceuri.getPath();

        int originalsize = 0;
        InputStream input = null;
        try {
            input = getContentResolver().openInputStream(sourceuri);

            Log.Logger().finest("input in profileview Activity" + input);

        } catch (FileNotFoundException e) {
            e.printStackTrace();

            filenotfoundexecption = true;

        }

        try {
            originalsize = input.available();

            Log.Logger().finest(
                    "Profile view activity originalsize" + originalsize);

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(input);
                bos = new BufferedOutputStream(new FileOutputStream(
                        destination, false));
                byte[] buf = new byte[originalsize];
                bis.read(buf);
                do {
                    bos.write(buf);
                } while (bis.read(buf) != -1);

            } catch (IOException e) {
                Mint.logException(e);

                filenotfoundexecption = true;

                return false;

            }

        } catch (NullPointerException e1) {
            Mint.logException(e1);
            filenotfoundexecption = true;
        }

        /*
         * String[] cmd = new String[] { "logcat", "-f", GridViewDemo_LOGPATH,
         * "-v", "time", "ActivityManager:W", "myapp:D" };
         * 
         * Runtime.getRuntime().exec(cmd);
         */

        return true;
    }