3

Welcome all

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 get's 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;
        }
    }

Thanks for your help

Community
  • 1
  • 1
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Try to put the URI in the intent as Data, not Extra. In addition remove the empty implementation of openAssetFile. – stan0 Feb 24 '14 at 17:27
  • 1
    I try to remove the implementation of openAssetFile and now the method is being called but it still doesn't works. I dont know how to put the URI in the intent as Data, that option does not exist. – NullPointerException Feb 25 '14 at 16:59
  • shareIntent.setData(theUri); ? – stan0 Feb 25 '14 at 17:01
  • 1
    I am experiencing the same issue. I implement both: `openAssetFile` and `openFile`, one for serving built-in assets and one for files in external storage. But `openAssetFile ` is always called when `openInputStream` is used against the URI. – Khanh Hua May 17 '15 at 11:02
  • I am running into this same issue, but when I try to change my call to `logsIntent.setData(uri)`, I get a "No apps can perform this action" message, whereas with the `logsIntent.putExtra(Intent.EXTRA_STREAM, uri)` I was at least getting the chooser to pop up. Any ideas? – TheIcemanCometh Nov 30 '15 at 03:38
  • @KhanhHua any finding for use both ? – Paraneetharan Saravanaperumal Jun 19 '20 at 03:56
  • @ParaneetharanSaravanaperumal you don't use both. You can use the `openAssetFile` and have it returned a non-asset file by constructing `new AssetFileDescriptor(ParcelFileDescriptor)` – Fadli Jun 29 '20 at 19:42
  • yes @AhmadFadli finally used two contenProvider :( – Paraneetharan Saravanaperumal Jul 01 '20 at 10:39

0 Answers0