Sending Image over Viber

2019-09-06 07:21发布

问题:

I am trying to send a image through viber or watzapp. Whatsapp works fine but viber always keep telling "The selected file appearers to be unsupported or corrupted. Please select a different File". Any idea what's going wrong ?

Here is my code

Uri uri = Uri.parse("android.resource://com.example.test/drawable/image_1");
                sharingIntent.setType("image/jpg");
                sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(sharingIntent, "Share image using"));

回答1:

int checkExistence = getResources().getIdentifier("image_"+position, "drawable", getPackageName());
                Bitmap bitmapToShare = BitmapFactory.decodeResource(
                        getResources(), checkExistence);
                File pictureStorage = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File noMedia = new File(pictureStorage, ".nomedia");
                if (!noMedia.exists())
                    noMedia.mkdirs();
                File file = new File(noMedia, "meme_shared_image.png");
                if (saveBitmapAsFile(bitmapToShare, file)) {
                    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.fromFile(file));
                    shareIntent.setType("image/jpeg");
                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    startActivity(Intent.createChooser(shareIntent, "Share image using"));
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Sending Error", Toast.LENGTH_LONG).show();
                }

            }

            private boolean saveBitmapAsFile(Bitmap bitmapToShare, File file) {
                FileOutputStream out;
                try {
                    out = new FileOutputStream(file);
                    bitmapToShare.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    return true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return false;
                }
            }

Fixed .. hope this help some one else at some time. how to share drawable image via viber and google hangout? This gave me the help