如何解决已解决找到文件但不共享
我有这个问题。我通过代码筛选我的手机并将其以 .jpg 格式保存在内存中。实际上手机会保存它(如果我从文件夹进入,我会看到文件)。但是当我去找它然后分享它时,它没有找到它。测试智能手机是搭载 Android 10 的三星 下面是代码。
MainActivity.java
private void shareIt(){
View view = findViewById(R.id.for_screen);
view.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bm = view.getDrawingCache();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
File picDir = new File(Environment.getExternalStorageDirectory() + File.separator + "Pic");
if (!picDir.exists())
{
picDir.mkdir();
}
File jpgFile = new File(picDir,"rrn" + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(jpgFile);
bm = Bitmap.createBitmap(bm,bm.getWidth(),(int)(bm.getHeight()/1.2));
boolean saved = bm.compress(Bitmap.CompressFormat.JPEG,100,fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printstacktrace();
} catch (IOException e) {
e.printstacktrace();
}
try {
Uri uriJpgFile = FileProvider.getUriForFile(this,"com.theb.generate.fileprovider",jpgFile);
shareIntent.putExtra(Intent.EXTRA_STREAM,uriJpgFile);
shareIntent.setType("image/jpg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(jpgFile.getAbsolutePath()));
startActivity(Intent.createChooser(shareIntent,"Condividi"));
} catch (Exception e) {
e.printstacktrace();
}
}
public void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE
);
}
}
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE
};
AndroidManifest.xml`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theb.generate">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Generatore"
android:requestLegacyExternalStorage="true">
<activity
android:name=".MainActivity"
android:screenorientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.theb.generate.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<Meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
我就是想不通我做错了什么。有人可以帮我吗?
编辑解决:
我通过调用文件并修改 Intents 解决了这个问题。下面是在 MainActivity.java -> shareIt() 中所做的修改。
private void shareIt(){
View view = findViewById(R.id.for_screen);
view.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bm = view.getDrawingCache();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
File picDir = new File(Environment.getExternalStorageDirectory() + File.separator + "Pic");
if (!picDir.exists())
{
picDir.mkdir();
}
File jpgFile = new File(picDir,fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printstacktrace();
}
try {
File jpg = new File(picDir,"rrn.jpg");
Uri uriJpgFile = FileProvider.getUriForFile(this,jpg);
if(jpg.exists()){
shareIntent.setType("image/jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM,uriJpgFile);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Et voilà");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Et voilà");
startActivity(Intent.createChooser(shareIntent,"Share File"));
}
} catch (Exception e) {
e.printstacktrace();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。