0

显现我只想将我手机中的图像发送到任何 android 应用程序,我使用了这个不起作用的代码。尝试分享到 whatsapp 时显示 Toast '文件不受支持'。

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            share_image();
        }
    });

private void share_image()
{
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/abc.jpg");
    Uri uri = FileProvider.getUriForFile(Main2Activity.this, BuildConfig.APPLICATION_ID + ".provider",file);
    Intent shareIntent = new Intent();
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
        shareIntent.addFlags( Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION );
    }
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/jpg");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
}


<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="schemas.android.com/apk/res/android"> 
 <external-path name="external_files" path="."/> 
</paths>
4

0 回答 0