代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
锁屏壁纸demo
作者:
zhuxian9397
/ 发布于
2013/10/23
/
809
选取一张图片,在裁剪为指定大小后保存到某目录,删除裁剪后的图片及数据库,通过FileObserver监视壁纸文件的变动,更新壁纸
package gp.yunfei.lockscreenwallpaper; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.app.ProgressDialog; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.FileObserver; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private static final int GET_IMAGE = 0; private static final int CROP_IMAGE = 1; private static final String TAG = "LockWallpaper"; private static final String LOCK_WALLPAPER = "lockwallpaper"; private ProgressDialog mWaitDialog; private FileObserver mFileObserver; private View mView; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { setBackground(); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mView = new View(this); setContentView(mView); mWaitDialog = new ProgressDialog(this); mWaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mWaitDialog.setMessage(getString(R.string.setting_lock)); mWaitDialog.setCancelable(false); mFileObserver = new FileObserver(getFilesDir().getAbsolutePath() + "/" + LOCK_WALLPAPER, FileObserver.CLOSE_WRITE) { @Override public void onEvent(int event, String path) { Log.d("gptest", "file event " + event + ", path = " + path); mHandler.sendEmptyMessage(1); } }; mFileObserver.startWatching(); String action = getIntent().getAction(); if (action.equals(Intent.ACTION_SET_WALLPAPER)) { Intent request = new Intent(Intent.ACTION_GET_CONTENT); request.setType("image/*"); startActivityForResult(request, GET_IMAGE); } else if (action.equals(Intent.ACTION_ATTACH_DATA)) { cropImage(getIntent().getData()); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_CANCELED) return; if (requestCode == GET_IMAGE) { cropImage(data.getData()); } else { Uri uri = data.getData(); if (uri != null) { setLockWallpaper(uri); } } } @Override protected void onDestroy() { mFileObserver.stopWatching(); super.onDestroy(); } private void cropImage(Uri uri) { DisplayMetrics d = getResources().getDisplayMetrics(); int width = d.widthPixels; int height = d.heightPixels; Intent i = new Intent("com.android.camera.action.CROP"); ComponentName cn = new ComponentName("com.google.android.gallery3d", "com.android.gallery3d.app.CropImage"); i.setComponent(cn) .setDataAndType(uri, "image/*") .putExtra("crop", "true") .putExtra("outputX", width) .putExtra("outputY", height) .putExtra("aspectX", width) .putExtra("aspectY", height) .putExtra("scale", true) .putExtra("scaleUpIfNeeded", true); startActivityForResult(i, CROP_IMAGE); } private void setBackground() { File f = new File(getFilesDir(), LOCK_WALLPAPER); if (!f.exists()) return; Drawable d = Drawable.createFromPath(f.getAbsolutePath()); mView.setBackgroundDrawable(d); } private void setLockWallpaper(final Uri uri) { new AsyncTask<Void, Void, Boolean>() { @Override protected void onPreExecute() { mWaitDialog.show(); } @Override protected void onPostExecute(Boolean result) { mWaitDialog.dismiss(); if (!result) Toast.makeText(MainActivity.this, "Set LockScreen Wallpaper failed!", Toast.LENGTH_SHORT).show(); //else //setBackground(); } @Override protected Boolean doInBackground(Void... arg0) { ContentResolver cr = getContentResolver(); String[] projection = new String[] {MediaStore.Images.ImageColumns.DATA}; Cursor c = null; FileInputStream fin = null; FileOutputStream fos = null; try { c = cr.query(uri, projection, null, null, null); if (c == null || c.getCount() <= 0) return false; c.moveToFirst(); File imageFile = new File(c.getString(0)); if (!imageFile.exists()) return false; File wallpaperFile = new File(getFilesDir(), LOCK_WALLPAPER); if (!wallpaperFile.exists()) { if (!wallpaperFile.createNewFile()) { return false; } } fin = new FileInputStream(imageFile); fos = new FileOutputStream(wallpaperFile); byte[] buffer = new byte[32768]; int amt; while ((amt=fin.read(buffer)) > 0) { fos.write(buffer, 0, amt); } if (imageFile.delete()) { cr.delete(uri, null, null); } } catch (Exception e) { Log.d(TAG, "set lockwallpaper failed! ", e); return false; } finally { try { if (c != null) c.close(); if (fin != null) fin.close(); if (fos != null) fos.close(); } catch (IOException e) { } } return true; } }.execute(); } }
试试其它关键字
锁屏
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
zhuxian9397
贡献的其它代码
(
1
)
.
锁屏壁纸demo
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3