Java Code Examples for android.net.Uri#fromFile()

The following examples show how to use android.net.Uri#fromFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From CloudPan with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCamera = (TextView) findViewById(R.id.camera);
    mAlbum = (TextView) findViewById(R.id.album);
    mPicture = (ImageView) findViewById(R.id.picture);
    mPicture2 = (ImageView) findViewById(R.id.picture2);

    mCamera.setOnClickListener(this);
    mAlbum.setOnClickListener(this);

    mCameraUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/camera.png"));
    mAlbumFile = new File(Environment.getExternalStorageDirectory() + "/album.png");

}
 
Example 2
Source File: ApkUtil.java    From AppUpdate with Apache License 2.0 6 votes vote down vote up
/**
 * 安装一个apk
 *
 * @param context     上下文
 * @param authorities Android N 授权
 * @param apk         安装包文件
 */
public static void installApk(Context context, String authorities, File apk) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    Uri uri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = FileProvider.getUriForFile(context, authorities, apk);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        uri = Uri.fromFile(apk);
    }
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
}
 
Example 3
Source File: AgentWebX5Utils.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
static Uri getUriFromFile(Context context, File file) {
        Uri uri = null;

//        LogUtils.i("Info", "::" + context.getApplicationInfo().targetSdkVersion + "   INT:" + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = getUriFromFileForN(context, file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }
 
Example 4
Source File: UserDataActivity.java    From ToDoList with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        //调用系统相机申请拍照权限回调
        case CAMERA_PERMISSIONS_REQUEST_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (hasSdcard()) {
                    imageUri = Uri.fromFile(fileUri);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                        imageUri = FileProvider.getUriForFile(UserDataActivity.this, "com.example.fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri
                    PhotoUtils.takePicture(this, imageUri, CODE_CAMERA_REQUEST);
                } else {
                    Toasty.info(this, "设备没有SD卡", Toast.LENGTH_SHORT, true).show();
                }
            } else {
                Toasty.info(this, "请允许打开相机", Toast.LENGTH_SHORT, true).show();
            }
            break;


        }
        //调用系统相册申请Sdcard权限回调
        case STORAGE_PERMISSIONS_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                PhotoUtils.openPic(this, CODE_GALLERY_REQUEST);
            } else {

                Toasty.info(this, "请允许操作SD卡", Toast.LENGTH_SHORT, true).show();
            }
            break;
        default:
    }
}
 
Example 5
Source File: OpenFileUtil.java    From GreenDamFileExploere with Apache License 2.0 5 votes vote down vote up
public static Intent getAllIntent(String param) {

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(param));
        intent.setDataAndType(uri, "*/*");
        return intent;
    }
 
Example 6
Source File: UrlUtil.java    From FriendCircle with GNU General Public License v3.0 5 votes vote down vote up
public static Uri getUri(@NonNull final File file) {
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return FileProvider.getUriForFile(AppContext.getAppContext(), AUTHORITY, file);
        } else {
            return Uri.fromFile(file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: UIUtils.java    From rebootmenu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 打开文件(字面意思)
 *
 * @param context  {@link Context}
 * @param filePath 文件完整路径
 * @return 是否成功打开
 */
public static boolean openFile(Context context, String filePath) {
    File f = new File(filePath);
    Uri uri = Build.VERSION.SDK_INT < Build.VERSION_CODES.N
            ? Uri.fromFile(f)
            : FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".file_provider", f);
    try {
        context.startActivity(new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION)
                .setDataAndType(uri, getMimeTypeFromUrl(filePath)));
        return true;
    } catch (ActivityNotFoundException ignored) {
        return false;
    }
}
 
Example 8
Source File: ImagePickerActivity.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
/**
 * 跳转相机拍照
 */
private void showCamera() {

    if (isSingleType) {
        //如果是单类型选取,判断添加类型是否满足(照片视频不能共存)
        ArrayList<String> selectPathList = SelectionManager.getInstance().getSelectPaths();
        if (!selectPathList.isEmpty()) {
            if (MediaFileUtil.isVideoFileType(selectPathList.get(0))) {
                //如果存在视频,就不能拍照了
                Toast.makeText(this, getString(R.string.single_type_choose), Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    //拍照存放路径
    File fileDir = new File(Environment.getExternalStorageDirectory(), "Pictures");
    if (!fileDir.exists()) {
        fileDir.mkdir();
    }
    mFilePath = fileDir.getAbsolutePath() + "/IMG_" + System.currentTimeMillis() + ".jpg";

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
        uri = FileProvider.getUriForFile(this, ImagePickerProvider.getFileProviderName(this), new File(mFilePath));
    } else {
        uri = Uri.fromFile(new File(mFilePath));
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, REQUEST_CODE_CAPTURE);
}
 
Example 9
Source File: ShareUtil.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
public static void shareImage(Context context, File file) {
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
        uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file);
    } else {
        uri = Uri.fromFile(file);
    }
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setType("image/*");
    context.startActivity(Intent.createChooser(intent, "Share image via"));
}
 
Example 10
Source File: FileUtil.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * android获取一个用于打开音频文件的intent
 *
 * @param param
 * @return
 */
public static Intent getAudioFileIntent(String param) {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "audio/*");
    return intent;
}
 
Example 11
Source File: CBrowserWindow.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void onDownloadStart(Context context, String url, String userAgent,
                            String contentDisposition, String mimetype, long contentLength) {
    if (contentDisposition == null
            || !contentDisposition.regionMatches(true, 0, "attachment", 0, 10)) {
        Intent installIntent = new Intent(Intent.ACTION_VIEW);
        String filename = url;
        Uri path = Uri.parse(filename);
        if (path.getScheme() == null) {
            path = Uri.fromFile(new File(filename));
        }
        installIntent.setDataAndType(path, mimetype);
        installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (checkInstallApp(context, installIntent)) {
            try {
                context.startActivity(installIntent);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(context, EUExUtil.getString("can_not_find_suitable_app_perform_this_operation"), Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
    if (null != mDialog) {
        return;
    }
    mDialog = new EDownloadDialog(context, url);
    mDialog.userAgent = userAgent;
    mDialog.contentDisposition = contentDisposition;
    mDialog.mimetype = mimetype;
    mDialog.contentLength = contentLength;
    ECallback callback = new ECallback() {
        @Override
        public void callback(Object obj) {
            mDialog = null;
        }
    };
    mDialog.setDoneCallback(callback);
    mDialog.show();
}
 
Example 12
Source File: ExportActivity.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private void checkNfc() throws Exception {
    // Check for available NFC Adapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        mNfcAdapter.setBeamPushUrisCallback(this, this);
        File databaseFile = ResourcesManager.getInstance(this).getDatabaseFile();
        mFileUris[0] = Uri.fromFile(databaseFile);
    }
}
 
Example 13
Source File: FileUtils.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
/**
 * Convert File into Uri.
 *
 * @param file
 * @return uri
 */
public static Uri getUri(File file) {
    if (file != null) {
        return Uri.fromFile(file);
    }
    return null;
}
 
Example 14
Source File: IntentUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 获取安装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);
}
 
Example 15
Source File: FileProvider7.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
public static Uri getUriForFile(Context context, File file) {
    Uri fileUri = null;
    if (Build.VERSION.SDK_INT >= 24) {
        fileUri = getUriForFile24(context, file);
    } else {
        fileUri = Uri.fromFile(file);
    }
    return fileUri;
}
 
Example 16
Source File: Utils.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
/**
 * 扫描图片
 */
public static void galleryAddPic(Context context, File file) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    context.sendBroadcast(mediaScanIntent);
}
 
Example 17
Source File: FileUtil.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 打开图片资源
 *
 * @param context
 * @param file
 */
public static void openImageIntent(Context context, File file) {
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addCategory("android.intent.category.DEFAULT");
    intent.setDataAndType(path, "image/*");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(intent);
}
 
Example 18
Source File: OpenFiles.java    From qingyang with Apache License 2.0 5 votes vote down vote up
public static Intent getVideoFileIntent(File file) {
	Intent intent = new Intent("android.intent.action.VIEW");
	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	intent.putExtra("oneshot", 0);
	intent.putExtra("configchange", 0);
	Uri uri = Uri.fromFile(file);
	intent.setDataAndType(uri, "video/*");
	return intent;
}
 
Example 19
Source File: CameraLauncher.java    From reader with MIT License 4 votes vote down vote up
/**
 * Get image from photo library.
 *
 * @param quality           Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
 * @param srcType           The album to get image from.
 * @param returnType        Set the type of image to return.
 * @param encodingType 
 */
// TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do!
// TODO: Images from kitkat filechooser not going into crop function
public void getImage(int srcType, int returnType, int encodingType) {
    Intent intent = new Intent();
    String title = GET_PICTURE;
    croppedUri = null;
    if (this.mediaType == PICTURE) {
        intent.setType("image/*");
        if (this.allowEdit) {
            intent.setAction(Intent.ACTION_PICK);
            intent.putExtra("crop", "true");
            if (targetWidth > 0) {
                intent.putExtra("outputX", targetWidth);
            }
            if (targetHeight > 0) {
                intent.putExtra("outputY", targetHeight);
            }
            if (targetHeight > 0 && targetWidth > 0 && targetWidth == targetHeight) {
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
            }
            File photo = createCaptureFile(encodingType);
            croppedUri = Uri.fromFile(photo);
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri);
        } else {
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
        }
    } else if (this.mediaType == VIDEO) {
            intent.setType("video/*");
            title = GET_VIDEO;
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
    } else if (this.mediaType == ALLMEDIA) {
            // I wanted to make the type 'image/*, video/*' but this does not work on all versions
            // of android so I had to go with the wildcard search.
            intent.setType("*/*");
            title = GET_All;
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
    }
    if (this.cordova != null) {
        this.cordova.startActivityForResult((CordovaPlugin) this, Intent.createChooser(intent,
                new String(title)), (srcType + 1) * 16 + returnType + 1);
    }
}
 
Example 20
Source File: ImageUtil.java    From YiBo with Apache License 2.0 4 votes vote down vote up
public static Uri getImageUri(String path) {
	return Uri.fromFile(new File(path));
}