本文整理汇总了Java中android.support.v4.content.FileProvider.getUriForFile方法的典型用法代码示例。如果您正苦于以下问题:Java FileProvider.getUriForFile方法的具体用法?Java FileProvider.getUriForFile怎么用?Java FileProvider.getUriForFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.support.v4.content.FileProvider
的用法示例。
在下文中一共展示了FileProvider.getUriForFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: callCamera
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void callCamera() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getApplicationContext().getPackageName() + ".provider", file);
Intent CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
CameraIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
CameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(CameraIntent, 5);
}
示例2: openCameraWithOutput
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void openCameraWithOutput() {
String path = new File(Environment.getExternalStorageDirectory(), "ktools").getAbsolutePath();
if (!new File(path).exists()) {
new File(path).mkdirs();
}
outputImageFile = new File(path, System.currentTimeMillis() + ".png");
if (!outputImageFile.exists()) {
try {
outputImageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//兼容7.0
Uri contentUri = FileProvider.getUriForFile(
this,
BuildConfig.APPLICATION_ID,
outputImageFile
);
Log.d(TAG, "openCameraWithOutput: uri = " + contentUri.toString());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(Intent.EXTRA_MIME_TYPES, MimeTypeMap.getSingleton().getMimeTypeFromExtension("png"));
//指定输出路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE_WITHOUT_COMPRESS);
}
示例3: getInstallAppIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
/**
* 获取安装App(支持6.0)的意图
*
* @param file 文件
* @return intent
*/
public static Intent getInstallAppIntent(File file) {
if (file == null) return null;
Intent intent = new Intent(Intent.ACTION_VIEW);
String type;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
type = "application/vnd.android.package-archive";
} else {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FileUtils.getFileExtension(file));
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(Utils.getContext(), AppUtils.getAppPackageName(Utils.getContext()), file);
} else {
uri = Uri.fromFile(file);
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, type);
return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
示例4: sendCameraIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void sendCameraIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photo = null;
try {
photo = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if(photo != null){
Uri file = FileProvider.getUriForFile(this, "ir.hphamid.instagram.fileProvider", photo);
imageUri = Uri.fromFile(photo);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(takePictureIntent, IMAGE_CAPTURE);
}
}
}
示例5: do_add_video
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
@OnClick(R.id.fab_add_video)
public void do_add_video()
{
Intent capture = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (capture.resolveActivity(getPackageManager()) != null) {
try {
File videoFile = createFileName("traxyvid", ".mp4");
mediaUri = FileProvider.getUriForFile(this,
getPackageName() + ".provider", videoFile);
capture.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri);
startActivityForResult(capture, CAPTURE_VIDEO_REQUEST);
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例6: dispatchTakePictureIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void dispatchTakePictureIntent() throws IOException {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.naren.quire.provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
示例7: shareScreenshot
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void shareScreenshot() {
Bitmap bitmap = BitmapUtils.getBitmapFromView(binding.constraint);
File dir = getFilesDir();
File file = new File(dir, "font-screenshot.png");
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (IOException e) {
Log.e(TAG, "Failed to save screenshot");
Snackbar.make(binding.coord, R.string.failed_to_save_screenshot, Snackbar.LENGTH_SHORT).show();
return;
}
Uri uri = FileProvider.getUriForFile(TypesetterActivity.this,
"com.bignerdranch.android.typesetter.fileprovider",
file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
}
示例8: dispatchTakePictureIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
public Intent dispatchTakePictureIntent() throws IOException {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = createImageFile();
// Continue only if the File was successfully created
if (photoFile != null) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(mContext.getApplicationContext(), mContext.getPackageName() + ".file_provider", photoFile);
} else {
uri = Uri.fromFile(photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
takePictureIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加读写权限
}
}
return takePictureIntent;
}
示例9: getInstallAppIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
/**
* 获取安装App(支持6.0)的意图
*
* @param file 文件
* @return intent
*/
public static Intent getInstallAppIntent(File file)
{
if(file == null) return null;
Intent intent = new Intent(Intent.ACTION_VIEW);
String type;
if(Build.VERSION.SDK_INT < 23)
{
type = "application/vnd.android.package-archive";
}
else
{
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FileTool.getExtension(file));
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider
.getUriForFile(Easy.getContext(), "com.your.package.fileProvider", file);
intent.setDataAndType(contentUri, type);
}
intent.setDataAndType(Uri.fromFile(file), type);
return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
示例10: callCamera
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void callCamera() {
ContentValues values = new ContentValues();
File imagePath = new File(getFilesDir(), "covers");
if(!imagePath.exists()) imagePath.mkdir();
File newFile = new File(imagePath, System.currentTimeMillis() + "photo.png");
if (Build.VERSION.SDK_INT > 21) { //use this if Lollipop_Mr1 (API 22) or above
mPicturePath = FileProvider.getUriForFile(this, getPackageName()+".fileprovider", newFile);
} else {
mPicturePath = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
it.putExtra(MediaStore.EXTRA_OUTPUT, mPicturePath);
startActivityForResult(it, RC_CAMERA);
}
示例11: takePicture
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
public void takePicture(int returnType, int encodingType)
{
// Save the number of images currently on disk for later
this.numPics = queryImgDB(whichContentStore()).getCount();
// Let's use the intent and see what happens
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Specify file so that large image is captured and returned
File photo = createCaptureFile(encodingType);
this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
applicationId + ".provider",
photo));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
//We can write to this URI, this will hopefully allow us to write files to get to the next step
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (this.cordova != null) {
// Let's check to make sure the camera is actually installed. (Legacy Nexus 7 code)
PackageManager mPm = this.cordova.getActivity().getPackageManager();
if(intent.resolveActivity(mPm) != null)
{
this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1);
}
else
{
LOG.d(LOG_TAG, "Error: You don't have a default camera. Your device may not be CTS complaint.");
}
}
// else
// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
}
示例12: shareBitmapToOtherApp
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
public void shareBitmapToOtherApp(Bitmap bitmap, Activity activity) {
File file = new File(getCacheDir(), getString(R.string.cached_picture_filename));
File imageFile = ContextUtils.get().writeImageToFileJpeg(file, bitmap);
if (imageFile != null) {
Uri imageUri = FileProvider.getUriForFile(this, getString(R.string.app_fileprovider), imageFile);
if (imageUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setDataAndType(imageUri, getContentResolver().getType(imageUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
activity.startActivity(Intent.createChooser(shareIntent, getString(R.string.main__share_meme_prompt)));
}
}
}
示例13: openCamera
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void openCamera() {
tmpFile = new File(getCacheDir(), System.currentTimeMillis() + ".jpg");
Uri upload_temp_uri = FileProvider.getUriForFile(this, getString(R.string.authProvider), tmpFile);
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, upload_temp_uri);
startActivityForResult(mIntent, Camera);
}
示例14: getInstallAppIntent
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
/**
* 获取安装App(支持7.0)的意图
*
* @param file 文件
* @param authority 7.0及以上安装需要传入清单文件中的{@code <provider>}的authorities属性
* <br>参看https://developer.android.com/reference/android/support/v4/content/FileProvider.html
* @return intent
*/
public static Intent getInstallAppIntent(File file, String authority) {
if (file == null) return null;
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data;
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
data = Uri.fromFile(file);
} else {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
data = FileProvider.getUriForFile(Utils.getContext(), authority, file);
}
intent.setDataAndType(data, type);
return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
示例15: changePhotoActionSelected
import android.support.v4.content.FileProvider; //导入方法依赖的package包/类
private void changePhotoActionSelected(int position) {
if (position == 0) { //take photo
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File newFile = mPresenter.getTempImageFileForOwner();
Uri uri = FileProvider.getUriForFile(getActivity(), getString(R.string.file_provider), newFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
List<ResolveInfo> cameraActivities = getActivity().getPackageManager()
.queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo activity : cameraActivities) {
getActivity().grantUriPermission(
activity.activityInfo.packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(takePictureIntent, Constants.RESULT_PROFILE_IMAGE_TAKEN);
}
} else if (position == 1) { //select photo
Intent imageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageIntent.setType("image/*");
imageIntent.putExtra("return-data", true);
imageIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(
Intent.createChooser(imageIntent, getString(R.string.select_photo)),
Constants.RESULT_PROFILE_IMAGE_SELECTED);
}
}