代码之家  ›  专栏  ›  技术社区  ›  Parekh

在android中共享图像时出错

  •  3
  • Parekh  · 技术社区  · 9 年前

    在android中,我不能以jpeg和png格式共享图像。请帮助并更正我的代码

    每当我编写共享代码时,它都会给出异常“文件格式不受支持”
    这是我的代码:

    Uri imageUri = Uri.parse("android.resource://com.parekh.shareimage/drawable");
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        startActivity(shareIntent);
    
    1 回复  |  直到 9 年前
        1
  •  2
  •   Oleg Filimonov madhan kumar    9 年前

    将您的可绘制图像存储到内部存储器中,然后选择该图像。这条路会帮助你。

    try{
    
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.shareforma);
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File file = new File(extStorageDirectory, "forma.PNG");
            FileOutputStream outStream = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        String msgText = "Sample Message";
    
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setType("image/*");
    
        //set your message
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 
    
        String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
    
        File imageFileToShare = new File(imagePath);
    
        Uri uri = Uri.fromFile(imageFileToShare);
    
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    
        startActivity(Intent.createChooser(shareIntent, msgText));
    
    推荐文章