NullPointerException
NullPointerException

Reputation: 37721

Problems with ContentProvider

I have a content provider that must share images from getFilesDir folder. The problem is that the method openFile is never being called. What should I do to achieve it?

I'm using the solution proposed here, but with getFilesDir: Create and Share a File from Internal Storage

I'm sharing the image with this code:

    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, ""));

The problem is that the method openFile is never being called. Instead of it, it is called the method openAssetFile but I don't want that! I want that the method openFile gets called.

This is my content provider:

    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;
        }
    }

Upvotes: 3

Views: 1027

Answers (0)

Related Questions