Image Save to SdCard for 6.0.1 Android Version

别说谁变了你拦得住时间么 提交于 2020-03-06 08:55:30

问题


This code works correctly under 6.0.1 android version but if i run this application on 6.0.1 android devices , it will not save images to sd card. What i need to update for 6.0.1 devices ?

public void SaveImages(int a ,String b)
        {
            Bitmap bitmap = null;
            OutputStream output;
            if(a==0)
            {
                bitmap = BitmapFactory.decodeResource(getResources(),
                        R.drawable.image_0);
            }



File filepath = Environment.getExternalStorageDirectory();

        // Create a new folder in SD Card
        File dir = new File(filepath.getAbsolutePath()
                + "/Wallpapers/");
        dir.mkdirs();

        // Create a name for the saved image
        File file = new File(dir,b);

        // Show a toast message on successful save
        Toast.makeText(FullImageActivity.this, "Loading...",
                Toast.LENGTH_SHORT).show();
        Toast.makeText(FullImageActivity.this, "Image Saved to SD Card",
                Toast.LENGTH_SHORT).show();
        try {

            output = new FileOutputStream(file);

            // Compress into png format image from 0% - 100%
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
            output.flush();
            output.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        }

        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

回答1:


On Android 6.0+, you need to request runtime permission to write to external storage.


In order to request runtime permission to write to external storage:

public class MarshmallowPermission {
    public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;

    public MarshmallowPermission() {
    }

    public boolean checkPermissionForExternalStorage(Activity activity) {
        if(Build.VERSION.SDK_INT >= 23) {
            int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(result == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public void requestPermissionForExternalStorage(Activity activity) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(activity,
                    "External Storage permission needed. Please allow in App Settings for additional functionality.",
                    Toast.LENGTH_LONG).show();
            // user has previously denied runtime permission to external storage
        } else {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }
}

Then you can do

if(!marshmallowPermission.checkPermissionForExternalStorage(this)) {
    marshmallowPermission.requestPermissionForExternalStorage(this);
} else {
    // can write to external
}

And

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == MarshmallowPermission.EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE) {
        if(marshmallowPermission.checkPermissionForExternalStorage(this)) {
            // can write to external
        } else {
            // runtime permission denied, user must enable permission manually
        }
    }
}



回答2:


Refer the following link, How to save the image to SD card on button Click android. and Saving image from image view to sd card : Android. For detailed tutorial, http://www.android-examples.com/save-store-image-to-external-storage-android-example-tutorial/



来源:https://stackoverflow.com/questions/40742297/image-save-to-sdcard-for-6-0-1-android-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!