I have this problem. I screen my phone via code and save it in memory as a .jpg. Actually the phone saves it (If I enter from folder I see the file). But when I go to look for it and then share it, it doesn't find it. The test smartphone is a Samsung with Android 10
Here below the code.
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, 0, 0, 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>
I just can't figure out what I'm doing wrong. Can someone help me out here?
EDIT SOLVED:
I solved the problem by calling up the file and modifying the Intents. Below is the modification made in 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,"rrn" + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(jpgFile);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), (int)(bm.getHeight()/1.2));
boolean saved = bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
File jpg = new File(picDir, "rrn.jpg");
Uri uriJpgFile = FileProvider.getUriForFile(this, "com.theb.generate.fileprovider", 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();
}
}
question from:
https://stackoverflow.com/questions/65951663/solved-found-file-but-does-not-share-it 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…