ContentProvider的问题

时间:2014-02-24 16:47:37

标签: android share android-contentprovider

欢迎所有

我有一个内容提供商,必须从getFilesDir文件夹共享图像。问题是从不调用openFile方法。我该怎么做才能实现它?

我正在使用此处提出的解决方案,但使用getFilesDir:Create and Share a File from Internal Storage

我正在使用以下代码分享图片:

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    theUri = Uri.parse("content://com.myapp.cap/img319058");
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM,theUri);
    SectionManager.getCurrentActivity().startActivity(Intent.createChooser(shareIntent, ""));

问题是从不调用openFile方法。而不是它,它被称为方法openAssetFile,但我不希望这样!我想要openFile get方法叫做

这是我的内容提供商:

    public class AssetsContentProvider extends ContentProvider{ 
        @Override
        public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
              return null;
        }   

        @Override
          public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
              File root = getContext().getFilesDir();
              File cacheDir = getContext().getCacheDir();
              File path = new File(root, uri.getEncodedPath());
              path.mkdirs();
              File file = new File(path, "file_"+uri.getLastPathSegment());

              int imode = 0;
              if (mode.contains("w")) {
                imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
                if (!file.exists()) {
                  try {
                    file.createNewFile();
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }
              }
              if (mode.contains("r"))
                imode |= ParcelFileDescriptor.MODE_READ_ONLY;
              if (mode.contains("+"))
                imode |= ParcelFileDescriptor.MODE_APPEND;

              return ParcelFileDescriptor.open(file, imode);
          }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            return 0;
        }
        @Override
        public String getType(Uri uri) {
            return null;
        }
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            return null;
        }
        @Override
        public boolean onCreate() {
            return false;
        }
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            return null;
        }
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return 0;
        }
    }

感谢您的帮助

0 个答案:

没有答案