Skip to content

ctrl_c_v_code

kim han sung edited this page Nov 3, 2016 · 3 revisions
Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");

        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
        if (resInfo.isEmpty()) {
            return;
        }

        List<Intent> shareIntentList = new ArrayList<>();

        Uri uri = Uri.parse("file://"+Environment.getExternalStorageDirectory().toString()+ "/bandi_temp/10.jpg");
        intent.putExtra(Intent.EXTRA_STREAM,  uri);

        for (ResolveInfo info : resInfo) {
            Intent shareIntent = (Intent) intent.clone();

            if (info.activityInfo.packageName.toLowerCase().equals("com.facebook.katana")) {
                //facebook
                ComponentName componentName = new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "제목");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "구글 http://www.google.com #");
                shareIntent.setComponent(componentName);
                shareIntent.setPackage(info.activityInfo.packageName);
                shareIntentList.add(shareIntent);
            } else if(info.activityInfo.packageName.toLowerCase().equals("com.kakao.talk")) {
                ComponentName componentName = new ComponentName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "제목");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "구글 http://www.google.com #");
                shareIntent.setComponent(componentName);
                shareIntent.setPackage(info.activityInfo.packageName);
                shareIntentList.add(shareIntent);
            }
        }
        
        Intent chooserIntent = Intent.createChooser(shareIntentList.remove(0), "SNS에 공유하기");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntentList.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                } else {
                    finish();
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                break;
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
Clone this wiki locally