zoukankan      html  css  js  c++  java
  • 保存图片文件到本地

    从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);
                              //发送广播通知系统更新图库                          
                                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                Uri.parse("file://"+f.getAbsolutePath()))); 
    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>
     
  • 相关阅读:
    Python基础之文件、目录
    Python基础知识之基本类型、循环
    Python基础知识之函数、模块
    Python基础之小知识要点
    【Android】ADB常用指令与logcat日志(转)
    Android 中的 Service 全面总结 (转)
    Android实现双进程守护 (转)
    Android adb常见问题整理(转)
    Android代码内存优化建议-OnTrimMemory优化
    优化Android应用内存的若干方法
  • 原文地址:https://www.cnblogs.com/ws5861/p/3936623.html
Copyright © 2011-2022 走看看