Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a png image in drawable folder

I am integrating share with the following code for the app.

private void socialShare()
    {
        Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon");
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp");
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent, "Share from"));
    }

As in the above code, I am trying to put png image which is in drawable folder. But the image is unable to be sent. Is that because in setType, it's image/jpeg? I can't use jpeg, because it loses transparency. Can some one please suggest me how to share with image?

Here is the code that I use to copy the image from drawable to sdcard:

String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; 
        File direct = new File(commonPath);

        if(!direct.exists())
        {
            if(direct.mkdir()) 
              {
                Log.d("tag","directory created");
               //directory is created;
              }

        }

        Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.sharingimage);
        OutputStream outStream = null;
           File savingFile = new File(commonPath, "shareImage.png");
           if(!savingFile.exists())
           {
               Log.d("tag","file is created");

           try {
                savingFile.createNewFile();
                outStream = new FileOutputStream(savingFile);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();

                Log.d("tag","Saved");

               } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

               }

            }
like image 970
rick Avatar asked Aug 29 '13 04:08

rick


People also ask

How do I add an image to a drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.

What types of files can be stored in the RES drawable directory?

Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged). App icons, logos, and other graphics, such as those used in games, are well suited for this technique. To use an image resource, add your file to the res/drawable/ directory of your project.


1 Answers

No need for a permission. No need to implement a content provider.

This is what you need:

Manifest:

        <provider
            android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}"
            android:exported="false" android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>

res/xml/provider_paths.xml

<paths>
    <cache-path
        name="cache_root" path="." />
</paths>

And the code to use its Intent:

@WorkerThread
fun shareDrawable(context: Context, @DrawableRes resourceId: Int, fileName: String): Intent {
    val bitmap = ResourcesCompat.getDrawable(context.resources, resourceId, null)!!.toBitmap()
    val outputFile = File(context.cacheDir, "$fileName.png")
    val outPutStream = FileOutputStream(outputFile)
    bitmap.compress(CompressFormat.PNG, 100, outPutStream)
    outPutStream.flush()
    outPutStream.close()
    val shareIntent = Intent(Intent.ACTION_SEND)
    shareIntent.putExtra(Intent.EXTRA_STREAM, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        androidx.core.content.FileProvider.getUriForFile(context, context.packageName, outputFile)
    else
        Uri.fromFile(outputFile))
    shareIntent.type = "image/png"
    return shareIntent
}
like image 186
android developer Avatar answered Sep 17 '22 15:09

android developer