【问题标题】:Android Share Intent not workingAndroid共享意图不起作用
【发布时间】:2016-09-14 04:23:59
【问题描述】:

我正在尝试共享图像,它似乎仅在安装了 SD 卡或手机没有 SD 卡插槽时才有效。但是当我卸载 SD 卡时,它不会共享,它给了我两个错误。

  • 致命异常:main java.lang.NullPointerException:uriString

  • 无法插入图像 java.io.FileNotFoundException:没有这样的文件或目录

由于某种原因,它还保存了正在共享的图像,似乎无法弄清楚原因。

            private Button button;

        public void onCreate {

         init();
         setupView();
          }

        public void setupView(){
        button.setOnClickListener(this);
         }



         public void init() {
        button = (Button) findViewById(R.id.button);
          }




          @Override
           public void onClick(View v) {
           int id = v.getId();
           switch (id) {
           case R.id.button: {
              startShare();
               break;
             }




           public void startShare() {
        Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.m1);
             Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/*");
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path = MediaStore.Images.Media.insertImage(getContentResolver(),
            b, "Title", null);
        Uri imageUri =  Uri.parse(path);
        share.putExtra(Intent.EXTRA_STREAM, imageUri);
        startActivity(Intent.createChooser(share, "Share"));
         }

【问题讨论】:

标签: java android android-intent share mms


【解决方案1】:

尝试使用此代码共享可绘制图像:

 Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                            getResources().getResourcePackageName(R.drawable.ic_launcher) + '/' +
                            getResources().getResourceTypeName(R.drawable.ic_launcher) + '/' +
                            getResources().getResourceEntryName(R.drawable.ic_launcher));

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("image/*");
                    share.putExtra(Intent.EXTRA_STREAM, imageUri);
                    startActivity(Intent.createChooser(share, "Share"));

【讨论】:

  • 我尝试了该代码,但它不起作用,它显示“您无法将此图片添加到您的消息中”。
猜你喜欢
  • 1970-01-01
  • 2015-02-07
  • 2022-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多