skip to Main Content

I want to share image on share button. I use Glide to set image on ImageView. then I want to share particular image at that time I got The file format is not supported error.

private void shareContent(ImageView imageView) {
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    Bitmap bitmap =getBitmapFromView(imageView);
    try {
        File file = new File(context.getExternalCacheDir(),"logicchip.png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Intent.EXTRA_TEXT, "test");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        context.startActivity(Intent.createChooser(intent, "Share image via"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        bgDrawable.draw(canvas);
    }   else{
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return returnedBitmap;
}

REFERENCE IMAGE

3

Answers


  1. You should try to use the bitmap you have generated via glide, don’t try to take it from the view

    Login or Signup to reply.
  2. Uri.fromFile(file))

    Do not use Uri.fromFile() but use FileProvider to get an uri and serve your file.

    Login or Signup to reply.
  3. You can use this code for image sharing. also use path provider in your manifest.

     StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                StrictMode.setVmPolicy(builder.build());
                Intent imageshare= new Intent(Intent.ACTION_SEND);
                imageshare.setType("Image/*");
               
                Uri imageUri = FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", path);
                imageshare.putExtra(Intent.EXTRA_STREAM, imageUri );
                startActivity(Intent.createChooser(imageshare, "Pick an Image provider"));
       
    

    path provider in manifest inside application tag

    <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search