开发者

Android How do you save an image with your own unique Image Name?

This sounds like a issue a beginner like me would only have...this is my code...

    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);
}

This does save the image but when i go to look at the image on the sd card ti is called imag001 etc not the ID i am labelling it. How do i save the image with a name i want 开发者_开发技巧to call it? regards

EDIT

I found out something interesting...I believe on the SD card in the DCIM/100MEDIA the image taken by the application is stored using the name the phone has set as default. However, under data/data/com.andorid.packagename/files/ the image mayalso is being stored with the name i give to it.


Ostensibly, it seems as if you're setting the filename with the second line of code:

String strAvatarFilename =  Id + ".jpg";

I don't know where Id is coming from, you haven't included enough context for that, but it seems reasonable that it contains 'imag001'. To name your image something else, you need some other way to set that value. That could look hundreds of different ways.


The code you posted is looking fine, except one thing. The firstLine of the function. You never pass any id to this function so it use a global class variable (I guess so).

To solve your problem, you should change your function to something like that,

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

That way you always know where exactly does the id come from. In your current code, it's unclear where you get that Id and if it's even modified anywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜