android在sd卡中保存位图

时间:2016-07-15 16:22:01

标签: android bitmap

我使用以下代码将位图保存在SD卡中

String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);

        String fname = "Image" +".jpg";
       // String imageInSD1 = Environment.getExternalStorageDirectory().getAbsolutePath() +"/saved_images/" +  fname;
        File file = new File (myDir, fname);
       // if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap_profile1.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

它的工作正常,但问题是我在循环中这样做,每5秒运行一次图像慢慢慢慢地损坏。我想保存图像只有一次我怎么能检查这个如果图像已经在SD卡然后不保存图像我只有一个名称的图像图像更改但其名称保持相同请告诉我要保存一个图像只有一次如果位图值改变那么图像保存其他图像不能保存。

1 个答案:

答案 0 :(得分:0)

您正在寻找的功能是file.exists()

创建文件后,转到

// ContentList.aspx.cs
MySiteEntities db = new MySiteEntities();

my_ContentList = db.Content.Where(it => it.Language == "En" && it.Type == QueryType && it.Active == true && it.Deleted == false).OrderBy(it => it.Sort).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();

RowCount = db.Content.Where(it => it.Language == "En" && it.Type == QueryType && it.Active == true && it.Deleted == false).Count();

PageCount = RowCount / PageSize + 1;




// ContentList.aspx

<%if (RowCount > PageSize) {%>

   <%if (PageIndex > 1) {%><a href="Content/<%=Type%>/<%=PageIndex-1%>" class="page-numbers prev">Previous</a><%}%>

    <%
    int Position = 1;
    do 
    {
    %>
        <a href="Content/<%=Type%>/<%=Position%>" class="page-numbers <%if (Position == PageIndex) {%>current<%}%>"><%=Position%></a>
    <%
    Position++;
    } while (Position < PageCount+1);
    %>

   <%if (PageIndex != PageCount) {%><a href="Content/<%=Type%>/<%=PageIndex+1%>" class="page-numbers next">Next</a><%}%>

<%}%>

修改:完整代码

if(!file.exists())
    try{...
        ....