Sending Image over Viber

前提是你 提交于 2019-12-25 02:38:05

问题


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



来源:https://stackoverflow.com/questions/25802308/sending-image-over-viber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!