Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在onClick()之后使ImageView无效_Android_Android Imageview_Android View - Fatal编程技术网

Android 在onClick()之后使ImageView无效

Android 在onClick()之后使ImageView无效,android,android-imageview,android-view,Android,Android Imageview,Android View,我在活动中有一个onClick()方法,该onClick()方法更改ImageView的照片。我想使ImageView无效,并在onClick()之后重新绘制它,但是ImageView.invalidate()和ImageView.postInvalidate()以及view.invalidate()和view.postInvalidate()都不会导致ImageView刷新。相反,它会在下次单击时刷新。方法如下: public void onImageViewProfileClick(View

我在活动中有一个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();
    }
}

解决方案是将失效转移到onActivityResult()方法中。我在这里之后添加了无效声明:

        image = ThumbnailUtils.extractThumbnail(unscaledImage, IMAGE_SIZE,
                IMAGE_SIZE);
        imageViewProfile.setImageBitmap(image);
        imageViewProfile.postInvalidate();