保存图片文件到本地

从ImageView中获取图像并保存。

 1           saveImg.setOnClickListener(new OnClickListener() {
 2                 public void onClick(View v) {
 3                     // 新建一个文件保存照片
 4 //                     f = new File(GlobalData.Car273File + "/saved/" + System.currentTimeMillis() + ".jpg");
 6                      File f=getFilePath(GlobalData.Car273File + "/saved/", System.currentTimeMillis() + ".jpg");
 7                     try {
 8                         // ImageView对象(view)必须做如下设置后,才能获取其中的图像
 9                         itemView.setDrawingCacheEnabled(true);
10                         Bitmap temp = Bitmap.createBitmap(itemView
11                                 .getDrawingCache());
12                         itemView.setDrawingCacheEnabled(false);
13                         ImageUtil.saveMyBitmap(f, temp);
14                         //显示自定义toast
15                         Toast toast=new Toast(ShowCarImageActivity.this);
16                         View view=LayoutInflater.from(ShowCarImageActivity.this).inflate(R.layout.toast_custom_saved,null);
17                         toast.setDuration(Toast.LENGTH_SHORT);
18                         toast.setGravity(Gravity.CENTER,0, 100);
19                         toast.setView(view);
20                         toast.show();
21 //                        Car273Util.showToast(ShowCarImageActivity.this,
22 //                                "图片已保存到" + f.getParentFile().getAbsolutePath()
23 //                                        + "下");
24                     } catch (IOException e) {
25                         e.printStackTrace();
26                     }
27                     dismiss();
28                 }
29             });

此处要注意的是:在android4.0的手机上直接创建某个文件的路径一直报这个错:open failed: ENOENT (No such file or directory).例如第4行直接new出一个文件。

如果在FileOutputStream创建一个流文件路径时或者是对一个File文件路径直接操作时,可先创建文件的路径,然后在创建文件名就不会在报该错误。例如第6行的方式。

   public static File getFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return file;
    }

    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {

        }
    }
 1     /** 保存图片方法 **/
 2     public static void saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
 3         try {
 4
 5             f.createNewFile();
 6             FileOutputStream fOut = null;
 7             fOut = new FileOutputStream(f);
 8             int width = mBitmap.getWidth();
 9             int height = mBitmap.getHeight();
10             // 计算缩放的比例
11             int finalWidth = 800;
12             int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
13             double x = width * finalHeight;
14             double y = height * finalWidth;
15
16             if (x > y) {
17                 finalHeight = (int) (y / (double) width);
18             } else if (x < y) {
19                 finalWidth = (int) (x / (double) height);
20             }
21
22             if (finalWidth > width && finalHeight > height) {
23                 finalWidth = width;
24                 finalHeight = height;
25             }
26             Matrix matrix = new Matrix();
27             matrix.reset();
28             // 计算宽高缩放率
29             float scaleWidth = ((float) finalWidth) / (float) width;
30             float scaleHeight = ((float) finalHeight) / (float) height;
31             // 缩放图片动作
32             matrix.postScale(scaleWidth, scaleHeight);
33             mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width,
34                     (int) height, matrix, true);
35             mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
36             fOut.flush();
37             fOut.close();
38             // 回收内存空间
39             mBitmap.recycle();
40             System.gc();
41         } catch (FileNotFoundException e) {
42             e.printStackTrace();
43         } catch (IOException e) {
44             e.printStackTrace();
45         }
46     }
自定义Toast布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="133dp"
        android:layout_height="133dp"
        android:background="@drawable/toast_bg"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="23dp"
            android:layout_marginBottom="10dp"
            android:src="@drawable/saveimg_success" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:gravity="center"
            android:textSize="16sp"
            android:layout_marginBottom="15dp"
            android:text="@string/saved_picture_tosdcad"/>
    </LinearLayout>

</LinearLayout>
 
时间: 2024-07-31 12:16:58

保存图片文件到本地的相关文章

给大家分享web开发新手修改hosts文件实现本地域名访问的正确方法

1.如何正确修改hosts文件: 一般打开hosts文件里面都会有个示例,按照其格式修改即可 比如以下内容: # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host 即代表打开rhino.acme.com这个网址将解析到102.54.94.97,ip地址与网址间至少有一空格,当然建议通过按Table键来编辑,即美观又不容易编写失误;这也就是通过解

Open SSH 登陆远程主机,怎样下载文件到本地?

许多人使用简易的SSH连接工具,有时候需要在SSH下复制文件到本地查看比较方便,这就用到了SCP命令. scp是有Security的文件copy,基于ssh登录,操作起来比较方便. 比如要把当前一个文件copy到远程另外一台主机上,可以如下命令. <span style="font-size:14px;">scp /home/example.tar.gz [email protected] host IP:/home/root</span> 如果想反过来操作,把

HDFS文件与本地文件的交互操作

1.在HDFS中创建一个新的文件夹,用于保存weblog_entries.txt hadoop fs -mkdir /data/weblogs 2.将weblog_entries.txt文件从本地文件系统复制到HDFS刚创建的新文件夹下 cd /home/data hadoop fs -copyFromLocal weblog_entries.txt /data/weblogs 3.列出HDFS上weblog_entries.txt文件的信息: hadoop fs –ls /data/weblo

SSH连接下复制远程linux服务器文件到本地的命令(zz)

原文链接 许多人使用简易的SSH连接工具,有时候需要在SSH下复制文件到本地查看比较方便,我给大家介绍一个简单的命令SCP. scp是有Security的文件copy,基于ssh登录.操作起来比较方便,比如要把当前一个文件copy到远程另外一台主机上,可以如下命令. scp /home/daisy/full.tar.gz [email protected]:/home/root 然后会提示你输入另外那台172.19.2.75主机的root用户的登录密码,接着就开始copy了. 如果想反过来操作,

PHP CURL实现远程下载文件到本地

<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($result); function httpcopy($url,$file='',$timeout=60){ $file=empty($file)?pathinfo($url,PATHINFO_BASENAME):$file; $dir=pathinfo($file,PATHINFO_DIRNAME); !i

大开测试:性能- 如何下载并保存文件到本地(连载18)

7.18  如何下载并保存文件到本地 1.问题提出 如何下载并保存文件到本地? 2.问题解答 一个人事管理系统项目一般都要实现能够上传和下载电子文件(如学位照.身份证.护照或者其他Word.Excel.Pdf等格式的电子文件),测试时为了模拟下载的场景,需要编写相关脚本.在HTTP中,没有任何一个方法或是动作能够标识“下载文件”这个动作,对HTTP来说,无论是下载文件或者请求页面,都只是发出一个GET请求,LoadRunner记录了客户端发出的对文件的请求,并能够收到文件内容.因此,完全可以通过

以plist文件做本地存储

1.把需要保存的信息已plist文件形式保存在本地,即写入沙盒: /** * 写入本地(plist文件) */ - (void)saveArray { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.新建数据 NSDictionary *dict = @

微信企业号上传媒体文件之本地文件上传

微信企业号上传媒体文件之本地文件上传 企业在使用接口时,对多媒体文件.多媒体消息的获取和调用等操作,是通过media_id来进行的. 通过接口https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE,企业可以上传多媒体文件. 注意,每个多媒体文件(media_id)会在上传到微信服务器3天后自动删除,以节省服务器资源. 通常文件上传是通过html表单进行的,通过HttpURLConne

js 保存文件到本地

原文http://blog.163.com/[email protected]/blog/static/87727415201310975054613/ js 保存文件到本地 2013-11-09 19:56:35| 分类: 默认分类 |举报|字号 订阅 var obj_target = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); if(obj_target)//非ie { obj_target.href = ‘1