2011-05-03 102 views
2

嗨我正在创建一个应用程序,使用我创建的相机应用程序制作照片,然后将这些图像存储在特定文件夹(PFC_Gallery)中。我遇到的问题是,我不知道如何更改存储的图像名称,而不是覆盖以前的图像。我尝试了一个变量imgCounter,每当我在下面的代码中看到一张照片时就会增加,但是当我关闭应用程序时,此变量将从0重新开始,然后我覆盖其他照片。如果有人知道任何解决方案,这将是非常有益的。更改存储图像的名称

public static int imgCounter = 0; 

@Override 
public void onPictureTaken(byte[] data, Camera camera) { 

    File path = Environment 
      .getExternalStoragePublicDirectory(("PFC_Gallery")); 
    File file = new File(path, "IMG" + imgCounter + ".jpg"); 
    imgCounter++; 

    try { 
     path.mkdirs(); 
     OutputStream os = new FileOutputStream(file); 
     os.write(data); 
     os.close(); 

     MediaScannerConnection.scanFile(this, 
       new String[] { file.toString() }, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
         Log.i("ExternalStorage", "Scanned " + path + ":"); 
         Log.i("ExternalStorage", "-> uri=" + uri); 
        } 
       }); 
    } catch (IOException e) { 
     Log.w("ExternalStorage", "Error writing " + file, e); 
    } 

} 

回答

0

听起来就像在关闭应用程序时将最后一个数字存储在首选项文件中一样。

在OnCreate中加载从设置的最后一个数字文件

SharedPreferences settings = getSharedPreferences("settings", 0); 
    int n = settings.getInteger("lastNumber", 0); 

和的onStop

SharedPreferences settings = getSharedPreferences("settings", 0); 
    SharedPreferences.Editor editor = settings.edit(); 
    editor.putInteger("lastNumber", n); 

    // Commit the edits! 
    editor.commit(); 

更多信息:http://developer.android.com/guide/topics/data/data-storage.html#pref

+0

它工作完美!谢谢!! – danizp 2011-05-07 14:31:01

0

保存imgCounter变量作为偏好和负载当你开始活动时。

相关问题