Android Studio Java中生成的二维码分享不显示问题求助
问题描述
尝试分享生成的二维码图片,但选择分享渠道后二维码并未显示:
开发功能:用户注册时生成二维码并通过弹窗展示,点击弹窗的Okay按钮后,将二维码通过用户选择的渠道分享出去。参考教程实现了分享功能,但生成的二维码无法在所选分享渠道中显示。
预期效果:
相关代码:
public void showQR() { Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.cutome_dialog_layout); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { dialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.custom_dialog_background)); } dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); dialog.setCancelable(false); //Optional dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //Setting the animations to dialog Button Okay = dialog.findViewById(R.id.btn_okay); Button Cancel = dialog.findViewById(R.id.btn_cancel); ImageView qr_image = dialog.findViewById(R.id.qrcode); MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); try { BitMatrix bitMatrix = multiFormatWriter.encode(studentNumber.getText().toString()+ " ," +fullName.getText().toString(), BarcodeFormat.QR_CODE, 300, 300); BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix); qr_image.setImageBitmap(bitmap); } catch (WriterException e) { throw new RuntimeException(e); } Okay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { BitmapDrawable bitmapDrawable = (BitmapDrawable) qr_image.getDrawable(); Bitmap bitmap = bitmapDrawable.getBitmap(); shareImageAndText(bitmap); } catch (Exception e) { Toast.makeText(RegisterStudent.this, "share image problem", Toast.LENGTH_SHORT).show(); } dialog.dismiss(); } }); Cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(RegisterStudent.this, "Cancel", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); dialog.show(); } private void shareImageAndText(Bitmap bitmap) { Uri uri = getImageToShare(bitmap); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.putExtra(Intent.EXTRA_TEXT, "Please dont share your QR Code to anyone"); intent.putExtra(Intent.EXTRA_SUBJECT, "QR Code"); intent.setType("images/*"); startActivity(Intent.createChooser(intent, "Share via")); } private Uri getImageToShare(Bitmap bitmap) { File folder = new File(getCacheDir(), "images"); Uri uri = null; try { folder.mkdirs(); File file = new File(folder, "image.jpg"); FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); uri = FileProvider.getUriForFile(this, "com.example.myapplication", file); } catch (Exception e) { e.printStackTrace(); } return uri; }
解决方案
问题根源
- 权限缺失:生成的图片Uri未授予分享目标应用读取权限,导致对方无法访问图片文件
- Intent类型兼容性:仅设置
images/*类型,部分应用无法同时识别图片和文本内容 - Bitmap获取风险:从ImageView的Drawable中获取Bitmap,可能因Dialog状态变化导致空值
具体修复步骤
1. 添加Uri读取权限
在启动分享Intent前,添加权限授予代码,确保接收应用能读取图片:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
2. 修正Intent类型
同时分享图片和文本时,使用更通用的MIME类型提升兼容性:
intent.setType("*/*");
3. 优化Bitmap获取逻辑
直接保存生成二维码时的Bitmap对象,避免从Drawable中获取的风险:
- 定义成员变量存储二维码Bitmap:
private Bitmap qrBitmap; - 生成时赋值:
qrBitmap = barcodeEncoder.createBitmap(bitMatrix); - 分享时直接使用该对象
修改后的showQR()关键片段:
private Bitmap qrBitmap; // 成员变量 public void showQR() { // ... 其他代码 ... try { BitMatrix bitMatrix = multiFormatWriter.encode(studentNumber.getText().toString()+ " ," +fullName.getText().toString(), BarcodeFormat.QR_CODE, 300, 300); BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); qrBitmap = barcodeEncoder.createBitmap(bitMatrix); qr_image.setImageBitmap(qrBitmap); } catch (WriterException e) { throw new RuntimeException(e); } Okay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if(qrBitmap != null){ shareImageAndText(qrBitmap); } else { Toast.makeText(RegisterStudent.this, "QR code not generated", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(RegisterStudent.this, "share image problem", Toast.LENGTH_SHORT).show(); } dialog.dismiss(); } }); // ... 其他代码 ... }
4. 检查FileProvider配置
确保AndroidManifest.xml中正确配置FileProvider:
<application> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.myapplication" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
同时res/xml/file_paths.xml需正确配置缓存目录:
<?xml version="1.0" encoding="utf-8"?> <paths> <cache-path name="images" path="images/" /> </paths>
修复后的shareImageAndText方法
private void shareImageAndText(Bitmap bitmap) { Uri uri = getImageToShare(bitmap); if(uri == null){ Toast.makeText(this, "Failed to create image file", Toast.LENGTH_SHORT).show(); return; } Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.putExtra(Intent.EXTRA_TEXT, "Please dont share your QR Code to anyone"); intent.putExtra(Intent.EXTRA_SUBJECT, "QR Code"); intent.setType("*/*"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(Intent.createChooser(intent, "Share via")); }
内容的提问来源于stack exchange,提问作者Mister BlueBanana
相关产品推荐
相关产品推荐

