Giter Site home page Giter Site logo

fitandroid7's Introduction

FitAndroid7

一行代码完成Android 7 FileProvider适配~

适配FileProvide需要声明provider,编写xml,以及在代码中做版本适配等...

可以抽取一个小库简化这些重复性操作,避免重复声明provider,编写xml,以及在代码中做版本适配...

使用

compile 'com.zhy.base:fileprovider:1.0.0'

通过FileProvider7这个类完成uri的获取即可,例如:

  • FileProvider7.getUriForFile
  • FileProvider7.setIntentDataAndType
  • FileProvider7.setIntentData

示例一 拍照

private static final int REQUEST_CODE_TAKE_PHOTO = 0x110;
private String mCurrentPhotoPath;

public void takePhotoNoCompress(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA)
                .format(new Date()) + ".png";
        File file = new File(Environment.getExternalStorageDirectory(), filename);
        mCurrentPhotoPath = file.getAbsolutePath();
	     // 仅需改变这一行
        Uri fileUri = FileProvider7.getUriForFile(this, file);

        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TAKE_PHOTO) {
        mIvPhoto.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
    }
    // else tip?

}

示例二 安装apk

public void installApk(View view) {
    File file = new File(Environment.getExternalStorageDirectory(),
            "testandroid7-debug.apk");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // 仅需改变这一行
    FileProvider7.setIntentDataAndType(this,
            intent, "application/vnd.android.package-archive", file, true);
    startActivity(intent);
}

fitandroid7's People

Contributors

hongyangandroid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fitandroid7's Issues

install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

app更新时,已经授予了读写权限,但是加上install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)这句后,android7.0就会报Permission Denial,提示没有授权;我去掉install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),app能够安装,但是安装完成后的页面直接闪退,不能提示用户打开app,请问这是什么原因呢;

@OverRide
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
Intent install = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(context, "cn.cloudworkshop.miaoding.fileprovider", file);
//授予读写权限
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// List resInfoList = context.getPackageManager()
// .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
// for (ResolveInfo resolveInfo : resInfoList) {
// String packageName = resolveInfo.activityInfo.packageName;
// context.grantUriPermission(packageName, contentUri,
// Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
// }

            install.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

        }
        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(install);
    }
}

裁剪遇到的问题

intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri); /////是file:/// 不报错 ,改成content://反而报错

java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri content://com.galaxyschool.app.wawaschool.fileprovider/external/lqwawa/wawatong/icon/zoom_icon.jpg from pid=20723, uid=10071 requires the provider be exported, or grantUriPermission()
请问 是啥原因呢

/**
* @param activity 当前activity
* @param orgUri 剪裁原图的Uri /////7.0必须是content://
* @param desUri 剪裁后的图片的Uri /////必须是file:///
* @param aspectX X方向的比例
* @param aspectY Y方向的比例
* @param width 剪裁图片的宽度
* @param height 剪裁图片高度
* @param requestCode 剪裁图片的请求码
*/
public static void startZoomPhoto(Activity activity, File orgUri, File desUri, int aspectX, int aspectY, int width, int height, int requestCode) {
doCrop(activity, FileProviderHelper.getUriForFile(activity,orgUri),FileProviderHelper.getUriForFile(activity,desUri),
aspectX, aspectY, width, height, requestCode);
}

private static void doCrop(Activity activity, Uri orgUri, Uri desUri, int aspectX, int aspectY, int width, int height, int requestCode) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    FileProviderHelper.setIntentDataAndType(activity,
            intent, "image/*", orgUri, true); /////7.0必须是content://
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", aspectX);
    intent.putExtra("aspectY", aspectY);
    intent.putExtra("outputX", width);
    intent.putExtra("outputY", height);
    intent.putExtra("scale", true);
    //将剪切的图片保存到目标Uri中
    intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);   /////是file:/// 不报错 ,改成content://反而报错
    intent.putExtra("return-data", false);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);

    activity.startActivityForResult(intent, requestCode);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.