Android Studio 通过 Intent 共享自定义视图图像无效
Android Studio sharing custom view image via intent not working
我在尝试通过社交媒体(Facebook 等...)通过单击按钮在我的一项活动中共享自定义视图时遇到了一些麻烦,但是当我按下按钮时没有任何反应并且 Logcat 是没有显示任何错误。自定义视图属于 canvas 绘图。下面是我的代码:
public class GraphActivity extends AppCompatActivity {
DrawGraphTest drawGraphTest;
private Button shareImage;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
drawGraphTest = (DrawGraphTest)findViewById(R.id.drawGraphTest);
shareImage = (Button) findViewById(R.id.btnShare);
shareImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
share(drawGraphTest);
}
});
}
@SuppressLint("SetWorldReadable")
public void share(View view){
Bitmap bitmap =getBitmapFromView(drawGraphTest);
try {
File file = new File(this.getExternalCacheDir(),"myImage.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GraphActivity">
<g.myPackage.DrawGraphTest
android:id="@+id/drawGraphTest"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982" />
我也在 Manifest.xml:
中启用了这些权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
点击按钮,Logcat详细显示:
2019-03-21 04:30:26.147 19176-19176/g.companieshouse.companieshouse D/errortag1: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/g.companieshouse.companieshouse/cache/companieshouse.png exposed beyond app through ClipData.Item.getUri()
作为参考,我尝试实现的代码在下面的 link 中,因为我是 Android Studio 的新手,所以我很难看到我在实现中哪里出了问题,并且真的需要一些帮助!我能看到的唯一区别是,在 link 中,他们共享一个图像视图,而我共享一个自定义视图。
应用程序向另一个应用程序公开 file:// Uri 时引发的异常。
不鼓励这种公开,因为接收应用可能无法访问共享路径。例如,接收应用程序可能没有请求 Manifest.permission.READ_EXTERNAL_STORAGE 运行时权限,或者平台可能正在跨用户配置文件边界共享 Uri。
相反,应用程序应使用 content:// Uris,以便平台可以扩展接收应用程序访问资源的临时权限。
这仅针对针对 Build.VERSION_CODES#N 或更高版本的应用程序抛出。允许针对早期 SDK 版本的应用程序共享 file:// Uri,但强烈建议不要这样做。
可能的解决方案:
如果android version > m,获取URI必须通过FileProvider,像这样
fun getUri(file: File): Uri? {
try {
return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
FileProvider.getUriForFile(MyApp.instance, MyApp.instance.packageName, file)
} else {
Uri.fromFile(file)
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
我在尝试通过社交媒体(Facebook 等...)通过单击按钮在我的一项活动中共享自定义视图时遇到了一些麻烦,但是当我按下按钮时没有任何反应并且 Logcat 是没有显示任何错误。自定义视图属于 canvas 绘图。下面是我的代码:
public class GraphActivity extends AppCompatActivity {
DrawGraphTest drawGraphTest;
private Button shareImage;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
drawGraphTest = (DrawGraphTest)findViewById(R.id.drawGraphTest);
shareImage = (Button) findViewById(R.id.btnShare);
shareImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
share(drawGraphTest);
}
});
}
@SuppressLint("SetWorldReadable")
public void share(View view){
Bitmap bitmap =getBitmapFromView(drawGraphTest);
try {
File file = new File(this.getExternalCacheDir(),"myImage.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GraphActivity">
<g.myPackage.DrawGraphTest
android:id="@+id/drawGraphTest"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982" />
我也在 Manifest.xml:
中启用了这些权限<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
点击按钮,Logcat详细显示:
2019-03-21 04:30:26.147 19176-19176/g.companieshouse.companieshouse D/errortag1: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/g.companieshouse.companieshouse/cache/companieshouse.png exposed beyond app through ClipData.Item.getUri()
作为参考,我尝试实现的代码在下面的 link 中,因为我是 Android Studio 的新手,所以我很难看到我在实现中哪里出了问题,并且真的需要一些帮助!我能看到的唯一区别是,在 link 中,他们共享一个图像视图,而我共享一个自定义视图。
应用程序向另一个应用程序公开 file:// Uri 时引发的异常。
不鼓励这种公开,因为接收应用可能无法访问共享路径。例如,接收应用程序可能没有请求 Manifest.permission.READ_EXTERNAL_STORAGE 运行时权限,或者平台可能正在跨用户配置文件边界共享 Uri。
相反,应用程序应使用 content:// Uris,以便平台可以扩展接收应用程序访问资源的临时权限。
这仅针对针对 Build.VERSION_CODES#N 或更高版本的应用程序抛出。允许针对早期 SDK 版本的应用程序共享 file:// Uri,但强烈建议不要这样做。
可能的解决方案:
如果android version > m,获取URI必须通过FileProvider,像这样
fun getUri(file: File): Uri? {
try {
return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
FileProvider.getUriForFile(MyApp.instance, MyApp.instance.packageName, file)
} else {
Uri.fromFile(file)
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
}