1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
| package com.geekmentors.wallpaperfreeapp.utils;
import android.app.WallpaperManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.geekmentors.wallpaperfreeapp.R;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
public class Utils {
private String TAG = Utils.class.getSimpleName();
private Context _context;
private PrefManager pref;
// constructor
public Utils(Context context) {
this._context = context;
pref = new PrefManager(_context);
}
/*
* getting screen width
*/
@SuppressWarnings("deprecation")
public int getScreenWidth() {
int columnWidth;
WindowManager wm = (WindowManager) _context
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) {
// Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}
public void reportImage(Bitmap bitmap)
{
File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
pref.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Uri uri = getImageContentUri(_context,file);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(_context);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/html");
intent.setData(Uri.parse("mailto:" + "info@geekmentors.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Wallpaper HD: Report Image");
intent.putExtra(Intent.EXTRA_TEXT, "Write Your Report Below...");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
Intent chooserIntent = Intent.createChooser(intent,
"Report Via");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
_context.startActivity(chooserIntent);
}catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void shareImage(Bitmap bitmap, CoordinatorLayout coordinatorLayout)
{
File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
pref.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Uri uri = getImageContentUri(_context,file);
// final Intent shareIntent = new Intent(Intent.ACTION_SEND);
// shareIntent.setType("image/jpg");
// shareIntent.putExtra(Intent.EXTRA_STREAM, uri.fromFile(file));
Intent wall_intent = new Intent(Intent.ACTION_SEND);
wall_intent.setType("image/*");
wall_intent.putExtra("mimeType", "image/*");
wall_intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
Intent chooserIntent = Intent.createChooser(wall_intent,
"Share Using");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
_context.startActivity(chooserIntent);
}catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Snackbar snackbar = Snackbar
.make(coordinatorLayout, _context.getString(R.string.toast_wallpaper_set_failed), Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
}
public void saveImageToSDCard(Bitmap bitmap, CoordinatorLayout coordinatorLayout) {
File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
pref.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
_context.getString(R.string.toast_saved).replace("#",
"\"" + pref.getGalleryName() + "\""), Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
Log.d(TAG, "Wallpaper saved to: " + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
Snackbar snackbar = Snackbar
.make(coordinatorLayout, _context.getString(R.string.toast_saved_failed), Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
}
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
public void setAsWallpaper(Bitmap bitmap, CoordinatorLayout coordinatorLayout) {
File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
pref.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Uri uri = getImageContentUri(_context,file);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(_context);
//Intent intent = new Intent(wallpaperManager.getCropAndSetWallpaperIntent(uri));
//_context.startActivity(intent);
Intent wall_intent = new Intent(Intent.ACTION_ATTACH_DATA);
wall_intent.setDataAndType(uri, "image/*");
wall_intent.putExtra("mimeType", "image/*");
Intent chooserIntent = Intent.createChooser(wall_intent,
"Set As");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
_context.startActivity(chooserIntent);
}catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Snackbar snackbar = Snackbar
.make(coordinatorLayout, _context.getString(R.string.toast_wallpaper_set_failed), Snackbar.LENGTH_SHORT);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
}
} |
Partager