当android手机背景照片删除后 找回依然存在的背景图片

本文介绍了一种使用Android代码从手机中恢复丢失的背景照片的方法,通过将背景图片保存在特定目录下,实现照片的找回。

今天我女朋友把手机把一张手机背景照片原稿删了,又没有备份。这下我遭殃了  非要我把这张照片弄出来,弄不出来跪键盘。本人参考了网上各大神的经验做了个apk。希望能帮到像我一样的苦命孩子


android手机背景照片原件删除后  背景不会改变。当修改背景照片时才会更改。因为在设置背景时系统复制的原件的副本隐藏于手机中。

以下android代码   把背景图片保存在/sdcard/Boohee中(就在文件游览器中找到Boohee文件夹  里面保存了桌面图片)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class FindImage extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private Button Button_1;
    private Drawable wallpaperDrawable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        initData();
    }

    public void initView() {
        Button_1 = (Button) findViewById(R.id.button_1);
        Button_1.setOnClickListener(this);
    }

    public void initData() {
        // 获取壁纸管理器
        final WallpaperManager wallpaperManager = WallpaperManager
                .getInstance(this);
        // 获取壁纸图片
        wallpaperDrawable = wallpaperManager.getDrawable();
        // 图片视图
        final ImageView imageView = (ImageView) findViewById(R.id.Image_View_1);
        // 绘制缓存
        imageView.setDrawingCacheEnabled(true);
        // 设置图片
        imageView.setImageDrawable(wallpaperDrawable);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button_1:
            saveImage(drawableToBitmap(wallpaperDrawable));
            break;
        }
    }

    public static File saveImage(Bitmap bmp) {
        File appDir = new File(Environment.getExternalStorageDirectory(),
                "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;

    }

}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<Button 
	    android:id="@+id/button_1"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="get"/>
	<ImageView
	    android:id="@+id/Image_View_1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:background="@drawable/star1"
	    />
</LinearLayout>
android应用  链接 http://pan.baidu.com/s/1pJG7LGn  拖到手机中安装即可

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值