當前位置: 首頁>>代碼示例>>Java>>正文


Java FileProvider.getUriForFile方法代碼示例

本文整理匯總了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);
    }
 
開發者ID:codingdojoangola,項目名稱:cda-app,代碼行數:19,代碼來源:ChatActivity.java

示例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);
}
 
開發者ID:jiangkang,項目名稱:KTools,代碼行數:27,代碼來源:ImageActivity.java

示例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);
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:26,代碼來源:IntentUtils.java

示例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);
        }
    }
}
 
開發者ID:hphamid,項目名稱:maktabkhoone-instagram,代碼行數:18,代碼來源:ShareImageActivity.java

示例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();
        }
    }
}
 
開發者ID:gvsucis,項目名稱:mobile-app-dev-book,代碼行數:17,代碼來源:JournalViewActivity.java

示例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);
            }
        }
    }
 
開發者ID:narenkukreja,項目名稱:quire,代碼行數:23,代碼來源:ListNewItemActivity.java

示例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);
}
 
開發者ID:bignerdranch,項目名稱:Typesetter,代碼行數:24,代碼來源:TypesetterActivity.java

示例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;
}
 
開發者ID:Sugarya,項目名稱:SugarPhotoPicker,代碼行數:22,代碼來源:ImageCaptureManager.java

示例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);
}
 
開發者ID:Ayvytr,項目名稱:EasyAndroid,代碼行數:32,代碼來源:IntentTool.java

示例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);
}
 
開發者ID:victoraldir,項目名稱:BuddyBook,代碼行數:18,代碼來源:InsertEditBookActivity.java

示例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");
    }
 
開發者ID:Andy-Ta,項目名稱:COB,代碼行數:34,代碼來源:CameraLauncher.java

示例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)));
        }
    }
}
 
開發者ID:gsantner,項目名稱:memetastic,代碼行數:16,代碼來源:App.java

示例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);
}
 
開發者ID:RanKKI,項目名稱:PSNine,代碼行數:8,代碼來源:ImagesGalleryActivity.java

示例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);
}
 
開發者ID:penghongru,項目名稱:Coder,代碼行數:23,代碼來源:IntentUtils.java

示例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);
    }
}
 
開發者ID:Q115,項目名稱:Goalie_Android,代碼行數:28,代碼來源:ProfileFragment.java


注:本文中的android.support.v4.content.FileProvider.getUriForFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。