我试图分享在imageview中显示的图像。 但是当共享时,它显示黑屏,并在提交时说“共享失败”
$(document).ready(function () {
$('#filesTable').DataTable({
"order": [[0, "desc"]],
"bProcessing": true,
"bServerSide": true,
"sServerMethod": "POST",
"sAjaxSource": "files.aspx/fillFileTable"
});
});
[WebMethod]
public static string fillFileTable()
{
DataTable dt = //...(get table from database)
JavaScriptSerializer js=new JavaScriptSerializer();
js.MaxJsonLength = int.MaxValue;
return js.Serialize(dt);
}
代码如下
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CAMERA" />
logcat的
collageImage.setImageBitmap(bmap1);
Drawable mDrawable = collageImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)mDrawable).getBitmap();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.png");
cachePath.mkdirs();
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/DCIM/Camera/image.png"));
Intent openThree = new Intent(createChooser(share, "Share Image"));
startActivity(openThree);
Toast.makeText(v.getContext(), "Selected: ", Toast.LENGTH_LONG).show();
Gradle详情如下
06-18 13:47:36.906 4429-4429/com.austurn.keikonew.keiko W/System.err: java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
at java.io.File.createNewFile(File.java:948)
06-18 13:47:36.907 4429-4429/com.austurn.keikonew.keiko W/System.err: at com.austurn.keikonew.keiko.ThreeFragment$10.onClick(ThreeFragment.java:2122)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
06-18 13:47:36.909 4429-4429/com.austurn.keikonew.keiko W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
06-18 13:47:36.911 4429-4429/com.austurn.keikonew.keiko W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-18 13:47:37.034 4429-4440/com.austurn.keikonew.keiko I/art: Background partial concurrent mark sweep GC freed 1063(81KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 122MB/126MB, paused 26.566ms total 119.644ms
06-18 13:47:37.121 4429-4452/com.austurn.keikonew.keiko D/EGL_emulation: eglMakeCurrent: 0xa43052a0: ver 3 1 (tinfo 0xa43032a0)
06-18 13:47:40.471 4429-4452/com.austurn.keikonew.keiko D/EGL_emulation: eglMakeCurrent: 0xa43052a0: ver 3 1 (tinfo 0xa43032a0)
它适用于以前的版本和Android Studio早期版本。但问题发生在版本更新之后
答案 0 :(得分:1)
对于上面的代码,您需要应用运行时读取和写入权限,并且您的目标是Nougat,这就是您需要使用文件提供程序应用读取路径权限的原因。并需要在Android Manifest中注册提供程序
选中此link
答案 1 :(得分:0)
为Android 6.0(API级别23)及以上添加运行时权限代码,此处为official doc或您可以找到example here
这是WRITE_EXTERNAL_STORAGE
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"Permission is granted");
return true;
}
请求像这样的其他许可
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
答案 2 :(得分:0)
您必须为更新版本执行此操作: 例如:
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
else {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
profile_file_name = "profile_" + System.currentTimeMillis() + ".jpg";
File f = new File(android.os.Environment.getExternalStorageDirectory(), profile_file_name);
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getApplicationContext(), "com.example.hp.imageuploadapp.provider", f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, 1);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
profile_file_name = "profile_" + System.currentTimeMillis() + ".jpg";
File f = new File(android.os.Environment.getExternalStorageDirectory(), profile_file_name);
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getApplicationContext(), "com.example.hp.imageuploadapp.provider", f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, 1);}
else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
并在清单文件中添加以上内容
答案 3 :(得分:0)
谢谢大家给我一些线索。我进行了全部搜索,并使其正常运行。我的代码如下
Bitmap bitmap = ((BitmapDrawable)mDrawable).getBitmap();
if (Build.VERSION.SDK_INT >= 23)
{
if (checkPermission())
{
try {
File file = new File(getActivity().getExternalCacheDir(),"logicchip.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(android.content.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();
}
} else {
requestPermission(); // Code for permission
}
}
else
{
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(getActivity(), "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
}
}