我的应用程序适用于API 19,但不适用于API 24

时间:2018-05-20 11:48:48

标签: android android-layout

我的应用程序在api 19和api 24都运行良好,除非保存图像, 在api 19中,当点击保存按钮时,图像保存到图库中,但是当我在api 24中单击保存时,它不存储任何图像,并且它不会像api 19那样执行toast消息。 我的观点是:如何在不改变整个项目代码的情况下修改代码,以便在api 24中正常运行。

signature.java

  case R.id.action_download:
                File directory = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File file = new File(directory, System.currentTimeMillis() + ".png");
                FileOutputStream out = null;
                Bitmap bitmap = signatureView.getSignatureBitmap();
                try {
                    out = new FileOutputStream(file);
                    if (bitmap != null) {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    } else {
                        throw new FileNotFoundException();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (out != null) {
                            out.flush();
                            out.close();
                             //the bellow code does't work in api 24 ,but it       
                              works in api 19 to store image in galary.
                            if (bitmap != null) {
                                Toast.makeText(getApplicationContext(),
                                        "Image saved successfully at " + file.getPath(),
                                        Toast.LENGTH_LONG).show();
                                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                                    new MyMediaScanner(this, file);
                                } else {
                                    ArrayList<String> toBeScanned = new ArrayList<String>();
                                    toBeScanned.add(file.getAbsolutePath());
                                    String[] toBeScannedStr = new String[toBeScanned.size()];
                                    toBeScannedStr = toBeScanned.toArray(toBeScannedStr);
                                    MediaScannerConnection.scanFile(this, toBeScannedStr, null,
                                            null);
                                }
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return true;

gradle这个

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"

    defaultConfig {
        applicationId "com.kyanogen.signature"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 3
        versionName "1.0.2"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation project(":signature-view")
    implementation 'com.android.volley:volley:1.1.0'

}

1 个答案:

答案 0 :(得分:0)

添加全局变量:

private static final int REQUEST_PERMISSIONS_ALL = 70;

将您的源代码更改为:

case R.id.action_download:
    if (obtainedPermissions(this, true, REQUEST_PERMISSIONS_ALL)) {
        download();
    }
    return true;

覆盖方法:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSIONS_ALL) {
        if (obtainedPermissions(this, false, REQUEST_PERMISSIONS_ALL)) {
            download();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

请求权限和下载的方法:

public static boolean obtainedPermissions(Activity activity, boolean request, int requestCode) {
    if (Build.VERSION.SDK_INT >= 23) {
        ArrayList<String> permissions = new ArrayList<>();
        int hasPermissionReadExternalStorage = context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
        int hasPermissionWriteExternalStorage = context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (hasPermissionWriteExternalStorage != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (hasPermissionReadExternalStorage != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        if (permissions != null && !permissions.isEmpty()) {
            if (request) {
                String[] arrayPermission = permissions.toArray(new String[permissions.size()]);
                ActivityCompat.requestPermissions(activity, arrayPermission, requestCode);
            }
            return false;
        }
    }
    return true;
}


private void download() {
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File file = new File(directory, System.currentTimeMillis() + ".png");
    FileOutputStream out = null;
    Bitmap bitmap = signatureView.getSignatureBitmap();
    try {
        out = new FileOutputStream(file);
        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        } else {
            throw new FileNotFoundException();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.flush();
                out.close();
                 //the bellow code does't work in api 24 ,but it       
                  works in api 19 to store image in galary.
                if (bitmap != null) {
                    Toast.makeText(getApplicationContext(),
                            "Image saved successfully at " + file.getPath(),
                            Toast.LENGTH_LONG).show();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                        new MyMediaScanner(this, file);
                    } else {
                        ArrayList<String> toBeScanned = new ArrayList<String>();
                        toBeScanned.add(file.getAbsolutePath());
                        String[] toBeScannedStr = new String[toBeScanned.size()];
                        toBeScannedStr = toBeScanned.toArray(toBeScannedStr);
                        MediaScannerConnection.scanFile(this, toBeScannedStr, null,
                                null);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}