(已解决)找到文件但不共享

时间:2021-01-29 09:13:39

标签: image android-studio uri share filepath

我有这个问题。我通过代码筛选我的手机并将其以 .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, 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>

我就是想不通我做错了什么。有人可以帮我吗?

编辑解决:

我通过调用文件并修改 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,"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();
    }
}

0 个答案:

没有答案
相关问题