当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?
示例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)
日志:
android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外
编辑:
在定位Android Nougat时,file://
不再允许使用URI.我们应该使用content://
URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?
我想编写一个模块,只需单击按钮,相机就会打开,我可以单击并捕获图像.如果我不喜欢图像,我可以删除它并再单击一个图像,然后选择图像,它应该返回并在活动中显示该图像.
我想使用共享Intent共享一个文件(.pdf,.apk等),我搜索谷歌但我发现只有共享图像的代码
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Run Code Online (Sandbox Code Playgroud)
请帮忙解决这个问题
我有一个从 url 共享图像的应用程序。上次 android 更新,当我想在 instagram 提要中共享图像时,我收到了来自 instagram 的消息“无法加载图像”。
但我可以分享图片故事、直接消息和任何地方......我只有 instagram 提要有这个问题。
public void onShareItemOreo() {
// Get access to bitmap image from view
imageView = (ImageView) findViewById(R.id.thumbnail);
// Get access to the URI for the bitmap
Uri bmpUri = prepareShareIntent(imageView);
if (bmpUri != null) {
//outfile is the path of the image stored in the gallery
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setData(bmpUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT,marketLink);
// Launch sharing …
Run Code Online (Sandbox Code Playgroud) 我已经浏览了几个文档和堆栈,但是我不太确定如何实现这个......
帮助或示例代码真的可以帮助我了解更多。
这是运行相机的代码集,它运行得非常好,我的下一个问题是,如何让它自动保存到手机图库中?
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);
tvCameraTiles.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Get Camera Image File Data
// Bundle extras = data.getExtras();
// Bitmap imageBitmap = (Bitmap) …
Run Code Online (Sandbox Code Playgroud) 我在网上查看了很多代码,但似乎都遇到了问题。
使用以下函数创建并保存文件:
private static String filename = "eulerY.txt" ;
private void saveData() {
FileOutputStream fos_FILE_eulerY = null;
String message = "hello";
try {
fos_FILE_eulerY = openFileOutput(filename , MODE_PRIVATE);
fos_FILE_eulerY.write(message.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos_FILE_eulerY != null) {
try {
fos_FILE_eulerY.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// export data
sendEmail ();
}
Run Code Online (Sandbox Code Playgroud)
但是,当运行下面的代码发送文件时,我不断遇到问题 ClipData.Item.getUri
并按照建议使用此链接“/sf/ask/3368225801/”中的所有答案-item-geturi”,打开 Gmail 时,显示“无法附加文件”
private void sendEmail (){
File filelocation = new …
Run Code Online (Sandbox Code Playgroud) 我StrictMode.ThreadPolicy.Builder
从android文档StrictMode.ThreadPolicy.Builder引用。
我没有明确的想法StrictMode.ThreadPolicy.Builder
。
当我们必须使用此类时StrictMode.ThreadPolicy.Builder
。
有什么优势和目的StrictMode.ThreadPolicy.Builder
。
我想要详细的解释
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setThreadPolicy(policy);
Run Code Online (Sandbox Code Playgroud)