Share Image File from cache directory Via Intent~android
Asked Answered
G

3

25

I am trying to share image file in cache directory, i have the complete path, but not able to send the file in attachments, the code is

File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath()); 
*/


Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));

the above commented code is not working

the send mail shows attachment,but not receiving end there is not attachment, facebook sharing also shows no image in post

what the reason for this??

I have already seen the following SO Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android and many others, none of them are able to resolve the issue

P.S.;

1.The aim is to take screenshot of screen save it in cache directory and share it online from there

2.Yes i do have file, I can pull it via DDMS from device and see on system.

Galibi answered 30/9, 2013 at 14:5 Comment(6)
Cache directory is a private directory for your app. The sharing app will be unable to access the file.Trixie
Read developer.android.com/guide/topics/data/…, Saving files that should be sharedJustus
If you don't want to use external storage you need to create a ContentProvider to access the files wherever you keep them (even in the cache directory works)Justus
see #14865207 at SO, links to stephendnicholas.com/archives/974 for Content Provider, but that does not seems to be working as per SO post!!!Galibi
found android dev blog for share intents here android-developers.blogspot.in/2012/02/share-with-intents.html, explains a lotGalibi
You can copy the file to the external cache and share it that way.Garcon
F
14

what the reason for this?

As noted, other apps do not have access to your app's internal storage.

none of them are able to resolve the issue

Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.

but that does not seems to be working as per SO post!!!

Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.

Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.

Or, store your image on external storage, such as getExternalCacheDir().

Firelock answered 30/9, 2013 at 14:31 Comment(0)
D
69

I followed @CommonsWare's advice and used a FileProvider. Assuming your image is already in the internal cache directory as cache/images/image.png, then you can use the following steps. These are mostly a consolidation of the documentation.

Set up the FileProvider in the Manifest

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
</manifest>

Replace com.example.myapp with your app package name.

Create res/xml/filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="images/"/>
</paths>

This tells the FileProvider where to get the files to share (using the cache directory in this case).

Share the image

File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);

if (contentUri != null) {

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
    shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    startActivity(Intent.createChooser(shareIntent, "Choose an app"));

}

Documentation

Dhar answered 11/5, 2015 at 16:11 Comment(0)
F
14

what the reason for this?

As noted, other apps do not have access to your app's internal storage.

none of them are able to resolve the issue

Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.

but that does not seems to be working as per SO post!!!

Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.

Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.

Or, store your image on external storage, such as getExternalCacheDir().

Firelock answered 30/9, 2013 at 14:31 Comment(0)
S
0

I share cached image by followed steps .

1.Copy your cached image to target path.

public static File copyImage(String sourcePath, String targetPath){
        try {
               InputStream in = new FileInputStream(sourcePath);
               OutputStream out = new FileOutputStream(targetPath);
               byte[] buf = new byte[1024];
               int len;
               while ((len = in.read(buf)) > 0) {
                      out.write(buf, 0, len);
               }
               in.close();
               out.close();
               return new File(targetPath);
          } catch (IOException e) {
               e.printStackTrace();
               return null;
          }
  }

2.Get the Uri of copy file.

Uri uri = Uri.fromFile(target);

3.Share image by intent

    File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
    if(!dir.exists()){
        dir.mkdirs();
    }
    File f = new File(dir,"temporary_file.jpg");
    File target = copyImage(url,f.getAbsolutePath());
    Uri uri = Uri.fromFile(target);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM,uri );
    context.startActivity(intent);
Selle answered 17/8, 2015 at 8:13 Comment(1)
Yes,ok. But you still wind up having copy of file outside cache. That's was not what I was trying to do at that time. I finally created file outside in sdcard directory from start itself.Galibi

© 2022 - 2024 — McMap. All rights reserved.