Android Image Viewer from App

前端 未结 3 2075
闹比i
闹比i 2020-12-30 17:44

I\'m trying to launch an image which is written to my application directory with the builtin Android image viewer. This image has been written in a different part of the app

3条回答
  •  孤独总比滥情好
    2020-12-30 18:04

    One way is to implement a context provider to give other applications access to your data.

    Create a new class containing:

    public class FileContentProvider extends ContentProvider {
       private static final String URI_PREFIX = "content://uk.co.ashtonbrsc.examplefilecontentprovider";
    
       public static String constructUri(String url) {
           Uri uri = Uri.parse(url);
           return uri.isAbsolute() ? url : URI_PREFIX + url;
       }
    
       @Override
       public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
           File file = new File(uri.getPath());
           ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
           return parcel;
       }
    
       @Override
       public boolean onCreate() {
           return true;
       }
    
       @Override
       public int delete(Uri uri, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public String getType(Uri uri) {
       throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public Uri insert(Uri uri, ContentValues contentvalues) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
       @Override
       public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
       }
    
    }
    

    Add the content provider to your AndroidManifest.xml:

    
    

    You should then be able to use "content://uk.co.ashtonbrsc.examplefilecontentprovider/" + the full path to the image in your ACTION_VIEW intent.

提交回复
热议问题