简体   繁体   中英

Open Image from assets using external program

I've wrote content provider to open a png file in my app package with an external application (standard image viewer of Android). Image is stored in asset folder.

I cannot understand where is a problem, but it doesn't work for me.

openFile of ContentProvider:

   @Override
   public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
       URI file_uri = URI.create("file:///data/data/com.package/assets/image.png"); 
       File file = new File(file_uri.getPath());
       ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
       return parcel;
   }

Starting activity:

Uri uri = Uri.parse("file:///android_asset/image.png"); 
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);

Is this approach correct and where is my mistake? Or am I totally wrong?

The new Activity doesn't have access to your internal assets directory. You can either put the image on the sdcard or use your own ImageView that is part of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM