【问题标题】:Android Gallery doesn't support streamed filesAndroid Gallery 不支持流式文件
【发布时间】:2014-07-21 17:00:24
【问题描述】:

在我的应用中,用户可以选择文件并在适当的应用中打开它们(使用 ACTION_VIEW 意图)。 在将数据提供给其他应用程序之前,我需要对数据做一些工作。所以我使用的是流式解决方案:我实现了一个实现openTypedAssetFilewriteDataToPipe 的ContentProvider(这个方法填充了openPipeHelper 创建的输出ParcelFileDescriptor)。

这很有效:我可以打开 .pdf 文件、.txt 文件等。流式传输似乎是正确的。 我可以使用 3 方应用程序打开图像。 但是,当我使用 Gallery 打开图像时,它不起作用(图库显示黑屏),并且出现以下异常:

fail to open myfile.jpg
UriImage(21890): java.io.FileNotFoundException: Not a whole file

我查看了库源 (here),我可以看到这里抛出了异常:

        try {
            if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) {
                InputStream is = mApplication.getContentResolver()
                        .openInputStream(mUri);
                mRotation = Exif.getOrientation(is);
                Utils.closeSilently(is);
            }
            **mFileDescriptor = mApplication.getContentResolver()
                    .openFileDescriptor(mUri, "r");**
            if (jc.isCancelled()) return STATE_INIT;
            return STATE_DOWNLOADED;
        } catch (FileNotFoundException e) {
            Log.w(TAG, "fail to open: " + mUri, e);
            return STATE_ERROR;
        }

但是,一旦在图库应用程序中,如果我选择“设置为壁纸”,那么我可以看到我的图像,然后它就会被很好地流式传输。所以问题出现在画廊打开时。

经过深入研究(在 ContentResolver 代码等中),我无法理解它为什么会这样。 Gallery 似乎不支持流文件。那正确吗 ? 你有什么想法吗?

非常感谢。

【问题讨论】:

标签: android stream android-contentprovider android-contentresolver


【解决方案1】:
            String scheme = mUri.getScheme();

            ContentResolver contentResolver = getContentResolver();

            // If we are sent file://something or
            // content://org.openintents.filemanager/mimetype/something...
            if (scheme.equals("file")
                    || (scheme.equals("content") && fileUri
                            .getEncodedAuthority().equals(
                                    "org.openintents.filemanager"))) {

                // Get the path
                filePath = fileUri.getPath();

                // Trim the path if necessary
                // openintents filemanager returns
                // content://org.openintents.filemanager/mimetype//mnt/sdcard/xxxx.jpg
                if (filePath.startsWith("/mimetype/")) {
                    String trimmedFilePath = filePath
                            .substring("/mimetype/".length());
                    filePath = trimmedFilePath.substring(trimmedFilePath
                            .indexOf("/"));
                }
              } else if (scheme.equals("content")) {

                // If we are given another content:// URI, look it up in the
                // media provider

                filePath = getFilePathFromContentUri(fileUri,
                        contentResolver);

            } else {

                Log.e("filePath--------->>>>>", filePath + "");
            }

    private String getFilePathFromContentUri(Uri selectedVideoUri,
        ContentResolver contentResolver) {
    String filePathString;
    String[] filePathColumn = { MediaColumns.DATA };

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn,
            null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    filePathString = cursor.getString(columnIndex);
    cursor.close();
    return filePathString;
  }

现在使用这个文件路径..

   try {
        if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) {
            InputStream is = mApplication.getContentResolver()
                    .openInputStream(filePath);  // ** change is here 
            mRotation = Exif.getOrientation(is);
            Utils.closeSilently(is);
        }
        ** mFileDescriptor = mApplication.getContentResolver()
                .openFileDescriptor(filePath, "r"); **
        if (jc.isCancelled()) return STATE_INIT;
        return STATE_DOWNLOADED;
    } catch (FileNotFoundException e) {
        Log.w(TAG, "fail to open: " + mUri, e);
        return STATE_ERROR;
    }   

这对我有用

【讨论】:

  • 很抱歉,您的代码根本无法回答原始问题。最初的问题是关于将数据从一个应用程序流式传输到另一个应用程序以通过流传输它的 ContentProvider 打开文件。
猜你喜欢
  • 2016-10-03
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2013-12-12
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多