Android如何使用您自己独特的图像名称保存图像?

时间:2011-02-07 15:15:26

标签: android image

这听起来像像我这样的初学者只会...这是我的代码......

    private void saveAvatar(Bitmap avatar) {
    String strAvatarFilename =  Id + ".jpg"; 
    try {
        avatar.compress(CompressFormat.JPEG, 100, openFileOutput(strAvatarFilename, MODE_PRIVATE));
    } catch (Exception e) {
        Log.e(DEBUG_TAG, "Avatar compression and save failed.", e);
    }
    Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(PhotoActivity.this.getFilesDir(), strAvatarFilename));
    Editor editor = Preferences.edit();
    editor.putString(PREFERENCES_AVATAR, imageUriToSaveCameraImageTo.getPath());
    editor.commit();
    ImageButton avatarButton = (ImageButton) findViewById(R.id.ImageButton_Avatar);
    String strAvatarUri = Preferences.getString(PREFERENCES_AVATAR, "");
    Uri imageUri = Uri.parse(strAvatarUri);
    avatarButton.setImageURI(null); 
    avatarButton.setImageURI(imageUri);
}

这确实保存了图像但是当我去看sd卡上的图像时,ti被称为imag001等,而不是我标记它的ID。如何使用我想要调用的名称保存图像?问候

修改

我发现了一些有趣的东西......我相信DCIM / 100MEDIA中的SD卡,应用程序拍摄的图像是使用手机默认设置的名称存储的。但是,在data / data / com.andorid.packagename / files /下,图像也可能以我给它的名称存储。

2 个答案:

答案 0 :(得分:3)

表面上看,好像你是用第二行代码设置文件名:

String strAvatarFilename =  Id + ".jpg";

我不知道Id的来源,你没有包含足够的上下文,但它包含'imag001'似乎是合理的。要将图像命名为其他内容,您需要一些其他方法来设置该值。这看起来可能有数百种不同的方式。

答案 1 :(得分:1)

你发布的代码看起来很好,除了一件事。函数的第一行。你永远不会将任何id传递给这个函数,所以它使用全局类变量(我猜是这样)。

要解决您的问题,您应该将功能更改为类似的内容,

private void saveAvatar(String id, Bitmap avatar) {
    String strAvatarFilename =  id + ".jpg";
    ...
}

这样你总能知道id的确切来源。在您当前的代码中,不清楚您在哪里获得该ID以及它是否在任何地方都被修改过。