use of android.support.v4.content.FileProvider in project physical-web by google.
the class BluetoothSite method openInChrome.
private void openInChrome(File file) {
if (Utils.isGzippedFile(file)) {
file = Utils.gunzip(file, getTempFile());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri contentUri = new FileProvider().getUriForFile(activity, "org.physical_web.fileprovider", file);
activity.grantUriPermission("com.android.chrome", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "text/html");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
}
use of android.support.v4.content.FileProvider in project kdeconnect-android by KDE.
the class SharePlugin method onPackageReceived.
@Override
public boolean onPackageReceived(NetworkPackage np) {
try {
if (np.hasPayload()) {
Log.i("SharePlugin", "hasPayload");
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
} else if (permissionCheck == PackageManager.PERMISSION_DENIED) {
// TODO Request Permission for storage
Log.i("SharePlugin", "no Permission for Storage");
return false;
}
final InputStream input = np.getPayload();
final long fileLength = np.getPayloadSize();
final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis()));
//We need to check for already existing files only when storing in the default path.
//User-defined paths use the new Storage Access Framework that already handles this.
final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context);
final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath();
final String filename = customDestination ? originalFilename : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename);
String displayName = FilesHelper.getFileNameWithoutExt(filename);
final String mimeType = FilesHelper.getMimeTypeFromFile(filename);
if ("*/*".equals(mimeType)) {
displayName = filename;
}
final DocumentFile destinationFolderDocument = ShareSettingsActivity.getDestinationDirectory(context);
final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType, displayName);
final OutputStream destinationOutput = context.getContentResolver().openOutputStream(destinationDocument.getUri());
final Uri destinationUri = destinationDocument.getUri();
final int notificationId = (int) System.currentTimeMillis();
Resources res = context.getResources();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentTitle(res.getString(R.string.incoming_file_title, device.getName())).setContentText(res.getString(R.string.incoming_file_text, filename)).setTicker(res.getString(R.string.incoming_file_title, device.getName())).setSmallIcon(android.R.drawable.stat_sys_download).setAutoCancel(true).setOngoing(true).setProgress(100, 0, true);
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());
new Thread(new Runnable() {
@Override
public void run() {
boolean successful = true;
try {
byte[] data = new byte[1024];
long progress = 0, prevProgressPercentage = 0;
int count;
while ((count = input.read(data)) >= 0) {
progress += count;
destinationOutput.write(data, 0, count);
if (fileLength > 0) {
if (progress >= fileLength)
break;
long progressPercentage = (progress * 10 / fileLength);
if (progressPercentage != prevProgressPercentage) {
prevProgressPercentage = progressPercentage;
builder.setProgress(100, (int) progressPercentage * 10, false);
NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());
}
}
//else Log.e("SharePlugin", "Infinite loop? :D");
}
destinationOutput.flush();
} catch (Exception e) {
successful = false;
Log.e("SharePlugin", "Receiver thread exception");
e.printStackTrace();
} finally {
try {
destinationOutput.close();
} catch (Exception e) {
}
try {
input.close();
} catch (Exception e) {
}
}
try {
Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath());
//Update the notification and allow to open the file from it
Resources res = context.getResources();
String message = successful ? res.getString(R.string.received_file_title, device.getName()) : res.getString(R.string.received_file_fail_title, device.getName());
builder.setContentTitle(message).setTicker(message).setSmallIcon(android.R.drawable.stat_sys_download_done).setAutoCancel(true).setProgress(100, 100, false).setOngoing(false);
// TODO use FileProvider for >Nougat
if (Build.VERSION.SDK_INT < 24) {
if (successful) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(destinationUri, mimeType);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentText(res.getString(R.string.received_file_text, destinationDocument.getName())).setContentIntent(resultPendingIntent);
}
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("share_notification_preference", true)) {
builder.setDefaults(Notification.DEFAULT_ALL);
}
NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());
if (successful) {
if (!customDestination && Build.VERSION.SDK_INT >= 12) {
Log.i("SharePlugin", "Adding to downloads");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.addCompletedDownload(destinationUri.getLastPathSegment(), device.getName(), true, mimeType, destinationUri.getPath(), fileLength, false);
} else {
//Make sure it is added to the Android Gallery anyway
MediaStoreHelper.indexFile(context, destinationUri);
}
}
} catch (Exception e) {
Log.e("SharePlugin", "Receiver thread exception");
e.printStackTrace();
}
}
}).start();
} else if (np.has("text")) {
Log.i("SharePlugin", "hasText");
String text = np.getString("text");
if (Build.VERSION.SDK_INT >= 11) {
ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(text);
} else {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
Toast.makeText(context, R.string.shareplugin_text_saved, Toast.LENGTH_LONG).show();
} else if (np.has("url")) {
String url = np.getString("url");
Log.i("SharePlugin", "hasUrl: " + url);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (openUrlsDirectly) {
context.startActivity(browserIntent);
} else {
Resources res = context.getResources();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(browserIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new NotificationCompat.Builder(context).setContentTitle(res.getString(R.string.received_url_title, device.getName())).setContentText(res.getString(R.string.received_url_text, url)).setContentIntent(resultPendingIntent).setTicker(res.getString(R.string.received_url_title, device.getName())).setSmallIcon(R.drawable.ic_notification).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti);
}
} else {
Log.e("SharePlugin", "Error: Nothing attached!");
}
} catch (Exception e) {
Log.e("SharePlugin", "Exception");
e.printStackTrace();
}
return true;
}
use of android.support.v4.content.FileProvider in project BlogSource by TeachCourse.
the class PhotoUtil method goToTakePhoto.
/**
* 调用系统相机
*/
private void goToTakePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
photoFile = new File(photoFile, System.currentTimeMillis() + ".jpg");
photoPath = photoFile.getAbsolutePath();
if (Build.VERSION.SDK_INT > 23) {
/**
*Android 7.0以上的方式*
*/
String authority = activity.getApplicationContext().getPackageName() + ".fileProvider";
Uri contentUri = getUriForFile(activity, authority, photoFile);
activity.grantUriPermission(activity.getPackageName(), contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else {
/**
*Android 7.0以下的方式*
*/
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
activity.startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
}
use of android.support.v4.content.FileProvider in project BlogSource by TeachCourse.
the class WriteToReadActivity method installApk.
/**
* 安装应用程序
*
* @param file apk文件存放的路径
*/
private void installApk(File file) {
file = new File(file, "load/");
file = new File(file, "92Recycle-release.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT > 23) {
/**
*Android 7.0以上的方式*
*/
String authority = getApplicationContext().getPackageName() + ".fileProvider";
Uri contentUri = getUriForFile(this, authority, file);
/**
*请求授予的下面这句话等同于:intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);*
*/
grantUriPermission("cn.teachcourse", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
/**
*Android 7.0以前的方式*
*/
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
startActivity(intent);
}
use of android.support.v4.content.FileProvider in project BlogSource by TeachCourse.
the class PickPhotoUtil method goToTakePhoto.
/**
* 调用系统相机
*/
private void goToTakePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
photoFile = new File(photoFile, System.currentTimeMillis() + ".jpg");
photoPath = photoFile.getAbsolutePath();
if (Build.VERSION.SDK_INT > 23) {
/**
*Android 7.0以上的方式*
*/
String authority = activity.getApplicationContext().getPackageName() + ".fileProvider";
Uri contentUri = getUriForFile(activity, authority, photoFile);
activity.grantUriPermission(activity.getPackageName(), contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
} else {
/**
*Android 7.0以下的方式*
*/
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
activity.startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
}
Aggregations