0

アクティビティ内に onClick() メソッドがあり、その onClick() メソッドが ImageView の写真を変更します。ImageView を無効にして onClick() の後に再描画したいのですが、imageView.invalidate() も imageView.postInvalidate() も view.invalidate() も view.postInvalidate() も ImageView をリフレッシュさせていません。代わりに、次にクリックしたときに更新されます。メソッドは次のとおりです。

public void onImageViewProfileClick(View view) {
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    // Use this to restrict items to those on the SD card:
    photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    startActivityForResult(photoPickerIntent, REQUEST_CODE);
    if (image != null) {
        imageViewProfile.setImageBitmap(image);
        view.postInvalidate();
    }
}

誰かが解決策を知っていますか?

完全な方法は次のとおりです。

package com.example.news_app.android.ui;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.example.news_app.android.DataManager;
import com.example.news_app.R;
import com.example.news_app.android.Settings;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class SettingsActivity extends Activity {
    private Settings appSettings = new Settings(this);
    private EditText editTextName;
    private EditText editTextAbout;
    private EditText editTextEmail;
    private EditText editTextPhone;
    private ImageView imageViewProfile;
    private Bitmap image = null;
    public static final int REQUEST_CODE = 1;
    // The image will be scaled to 32x32
    public static final int IMAGE_SIZE = 32;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        editTextName = (EditText) findViewById(R.id.editTextName);

        editTextAbout = (EditText) findViewById(R.id.editTextAbout);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPhone = (EditText) findViewById(R.id.editTextPhone);
        imageViewProfile = (ImageView) findViewById(R.id.imageViewProfile);
        editTextName.setText(appSettings.getName());
        editTextAbout.setText(appSettings.getAbout());
        editTextEmail.setText(appSettings.getEmail());
        editTextPhone.setText(appSettings.getPhone());
        Bitmap profile = appSettings.getProfile();
        if (profile != null) {
            imageViewProfile.setImageBitmap(profile);
        }
        if (appSettings.getName().equals("")) {
            Toast.makeText(this,
                    "You have to enter a name before you can post or comment",
                    Toast.LENGTH_LONG).show();
        }
    }

    public void onImageViewProfileClick(View view) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        // Use this to restrict items to those on the SD card:
        photoPickerIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        startActivityForResult(photoPickerIntent, REQUEST_CODE);
        if (image != null) {
            imageViewProfile.setImageBitmap(image);
            view.postInvalidate();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream inputStream = null;
            try {
                inputStream = getContentResolver().openInputStream(
                        selectedImage);
            } catch (FileNotFoundException e1) {
                Log.d("onActivityResult", "FileNotFoundException");
                e1.printStackTrace();
            }
            Bitmap unscaledImage = BitmapFactory.decodeStream(inputStream,
                    null, null);
            image = ThumbnailUtils.extractThumbnail(unscaledImage, IMAGE_SIZE,
                    IMAGE_SIZE);
            Log.d("Unscaled image byte count",
                    "Bytes: " + unscaledImage.getByteCount());
            Log.d("Scaled image byte count", "Bytes: " + image.getByteCount());
        }
    }

    public void onButtonSaveClick(View view) {
        String editTextNameInput = editTextName.getText().toString();
        String editTextAboutInput = editTextAbout.getText().toString();
        String editTextEmailInput = editTextEmail.getText().toString();
        String editTextPhoneInput = editTextPhone.getText().toString();
        Bitmap imageViewProfileInput = ((BitmapDrawable) imageViewProfile
                .getDrawable()).getBitmap();
        if (editTextNameInput != null) {
            appSettings.setName(editTextNameInput);
        }
        if (editTextAboutInput != null) {
            appSettings.setAbout(editTextAboutInput);
        }
        if (editTextEmailInput != null) {
            appSettings.setEmail(editTextEmailInput);
        }
        if (editTextPhoneInput != null) {
            appSettings.setPhone(editTextPhoneInput);
        }
        if (imageViewProfileInput != null) {
            appSettings.setProfile(imageViewProfileInput);
        }
        DataManager dataManager = new DataManager(this);
        dataManager.addUser(appSettings.getUserId(), appSettings.getName(),
                appSettings.getAbout(), appSettings.getEmail(),
                appSettings.getPhone());
        this.finish();
    }
}
4

1 に答える 1