我试图以明确的意图传递屏幕截图,但屏幕显示黑色截图(请参阅图像here)。一旦我点击分享,就会出现一个吐司说发送失败。这是捕获屏幕截图并将其发送到其他应用程序的代码:
public void getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
f = new File(this.getFilesDir(), "screenshotFile");
try {
if (!f.exists())
f.createNewFile();
} catch (IOexception e) {
e.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
此代码将数据发送到whatsapp:
public void shareWhatsapp(View view) {
try {
myVib.vibrate(50);
getScreenShot(view);
//String fileName = "screenshotFile";
//Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
try {
intent.setPackage("com.whatsapp");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "App not installed", Toast.LENGTH_SHORT).show();
}
//TODO: APP CAN CRASH HERE
if (position > 0) {
try {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
} catch (Exception e) {
e.printStackTrace();
} finally {
intent.putExtra(Intent.EXTRA_TEXT, Titles.get(position - 1) + ": " + Links.get(position - 1)); //position problems
}
} else {
try {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
} catch (Exception e) {
e.printStackTrace();
} finally {
intent.putExtra(Intent.EXTRA_TEXT, Titles.get(0) + ": " + Links.get(0)); //position problems
}
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
有人可以帮我这个吗?
答案 0 :(得分:0)
首先,您要将文件写入internal storage。第三方应用无法访问您应用的内部存储空间。
其次,您使用的是`public void switchFragment(){
FragmentManager fm = getActivity().getFragmentManager();
fragmentB= (FragmentB) fm.findFragmentById(R.id.fragmentB);
if (fragmentB == null) {
fragmentB = FragmentB.newInstance("My Fragment B");
fm.beginTransaction().
replace(R.id.fragmentA, fragmentB).commit();
}
}`
,starting to be discontinued。
您最安全的长期行动方针是have a ContentProvider
serve your file from its location on internal storage,然后使用与Uri.fromFile()
相关联的Uri
。