1

I have a problem for sharing data in assets folder and I wrote codes for doing it.but it's not work and i couldn't share anything. i want share images via all social network android applications. this is my code:

String path="file:///android_asset/"+share.jpg;
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    Uri uri=Uri.parse(path); 
    whatsappIntent.setType("image/*");
    whatsappIntent.putExtra(Intent.EXTRA_STREAM,uri);
    try {
        startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        //ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

How i can resolve it thanks

Danielson
  • 2,605
  • 2
  • 28
  • 51
sr.farzad
  • 665
  • 3
  • 8
  • 23

1 Answers1

1

file:///android_asset/ only works in your process. Your options are:

  1. Copy the asset to a file on external storage and share it.

  2. Copy the asset to a file on internal storage, add a FileProvider to your app, and share the image via a Uri from your FileProvider.

  3. Try my StreamProvider for sharing straight from assets.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • what do you mean the file "only works in your process"? For my use case, I have a .png image file that I would like to text via Android Messages app, from my app to another user of my app. The image is a simple screenshot of a CardView of data. Rather than copy to internal storage, can't I just share the image using a content Uri to the res/drawable directory where the image is saved? What is the advantage of saving to internal storage and then sharing? – AJW May 13 '20 at 04:14
  • @AJW: "what do you mean the file "only works in your process"?" -- I did not say that a file only works in your process. I said `file:///android_asset/` only works in your process. "can't I just share the image using a content Uri to the res/drawable directory where the image is saved?" -- that statement does not make a lot of sense to me, sorry. If by "res/drawable directory" you mean a directory on the device, then it already is written to storage (internal or external, most likely). – CommonsWare May 13 '20 at 10:41
  • @AJW: If by "res/drawable directory" you mean a directory on your development machine, and are referring to a resource that is packaged into your app, your next problem is "using a content Uri". There is no `ContentProvider` for that resource in the Android SDK. My `StreamProvider` specifically does not handle that, because I have no clue how screen density will affect the behavior of a `ContentProvider`. You are welcome to try to create one yourself, if you like. And `file:///android_asset/` is irrelevant, as your content is not an asset. – CommonsWare May 13 '20 at 10:43
  • Thanks for the reply. So would best bet be to create a Bitmap from the .png image and explicitly copy the Bitmap to internal storage? And then share from internal storage using FileProvider, UriFromFile(), intent and .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)? – AJW May 13 '20 at 19:51
  • 1
    @AJW: That would work. Or, package another copy of the image as an asset or raw resource and just copy its bytes to a file, then use `FileProvider` (faster than decoding and re-encoding the image). – CommonsWare May 13 '20 at 20:27
  • Understood. Use the methods described by BalusC here: https://stackoverflow.com/questions/7211161/convert-image-byte-to-a-file ? – AJW May 13 '20 at 20:43
  • @AJW: `IOUtils` is from an Apache library IIRC. And you won't need the `ByteArrayInputStream`, if you are going the asset/raw resource route, as you get an `InputStream` from Android. The general concept is sound, though. – CommonsWare May 13 '20 at 20:50
  • Ah, will research then. – AJW May 13 '20 at 20:52
  • Used InputStream and OutputStream to copy to a File...can confirm file is created in directory and can save it to development machine and open it. Can't load into ImageView and show on UI and black background appears on the copied image. Any thoughts on how to fix? Question posted here: https://stackoverflow.com/questions/61846723/android-copy-internal-storage-image-to-imageview?noredirect=1#comment109390722_61846723 – AJW May 17 '20 at 15:06