首页 > 解决方案 > 我无法在 android 10+ 上保存图片,但我保存在 android 9 或更低版本上

问题描述

大家好,我正在尝试保存在 android studio 的应用程序上生成的图像。图片保存在 android 7 上,但在 android 10 上它没有保存到画廊 0 中。我尝试了多种解决方案。

这里的代码`public void SaveClick(View view){ ConstraintLayoutsavingLayout =(ConstraintLayout) findViewById(R.id.layoutavatar); 文件文件 = saveBitMap(fut.this, SavingLayout); AlertDialog.Builder builder = new AlertDialog.Builder(fut.this);

    builder.setCancelable(true);
    builder.setTitle("AVATAR HAS BEEN SAVED");
    builder.setMessage("YOUR AVATAR HAS BEEN SAVED SUCCESSFULLY TO YOUR IMAGES DIRECTORY");


    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            alertTextView.setVisibility(View.VISIBLE);
        }
    });
    builder.show();
}

private File saveBitMap(Context context, View drawView){

    FileOutputStream outputStream = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File pictureFileDir = new File(sdCard.getAbsolutePath() + "/AMONGUS-AVATAR");
    pictureFileDir.mkdir();

    String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
    File pictureFile = new File(filename);
    Bitmap bitmap =getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
        oStream.flush();
        oStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue saving the image.");
    }
    scanGallery( context,pictureFile.getAbsolutePath());
    return pictureFile;
}

//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    }   else{
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.TRANSPARENT);
    }
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}


// used for scanning gallery
private void scanGallery(Context cntx, String path) {
    try {
        MediaScannerConnection.scanFile(cntx, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue scanning gallery.");
    }


}

我的安卓清单:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_INTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:requestLegacyExternalStorage="true"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme">

    <activity android:name=".Choosename"></activity>
    <activity android:name=".privacypolicy" />
    <activity android:name=".disclaimerfut" />
    <activity android:name=".fut" />
    <activity android:name=".Choosemenu" />
    <activity android:name=".Mainmenu" />
    <activity android:name=".MainActivity">
        <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.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>

标签: android

解决方案


当我将 graddle 文件的 targetSdk 版本从 30 更改为 29 时,它可以使用任何建议 defaultConfig { applicationId 'com.ybq.fut21cardbuilder' minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true


推荐阅读