setDrawable时从库错误中选择图像

时间:2016-02-14 12:10:13

标签: android

当我在imageview中加载图像时,我有一个空指针异常。我搜索了很多主题,但我找不到解决方案。我不知道为什么这不起作用:/啊另一个小bug,在我在类中创建一个Imageview参数之前我尝试使用像ImageView profilPicture =(ImageView)findViewById(R.id.profil_picture);但是这产生了一个“错误”android工作室告诉我创建方法......

所以有错误代码:

 Caused by: java.lang.NullPointerException
                                                                    at android.graphics.BitmapShader.<init>(BitmapShader.java:40)
                                                                    at god.repertoire.RoundImage.<init>(RoundImage.java:30)
                                                                    at god.repertoire.FragmentNoContact.onActivityResult(FragmentNoContact.java:83)

我的片段有代码:

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


public class FragmentNoContact extends Fragment {
    private static int RESULT_LOAD_IMG = 1;
    ImageView profilPicture;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // GET FRAGMENT COMPONENT
        View myView = inflater.inflate(R.layout.activity_no_contact, container, false);
        FloatingActionButton addContact = (FloatingActionButton) myView.findViewById(R.id.button_add);
        profilPicture = (ImageView) myView.findViewById(R.id.profil_picture);


        // INIT PHOTO DE PROFIL
        Bitmap img = SaveLoad.loadFromFile("profil_picture.jpg");
        if (img == null)
            img = BitmapFactory.decodeResource(getResources(), R.drawable.blank_profile);
        RoundImage roundedImage = new RoundImage(img);
        profilPicture.setImageDrawable(roundedImage);

        // EVENT AU CLIC PHOTO
        profilPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
                /* an example i used before to test if i can set an another image
                ImageView profilPicture = (ImageView) v.findViewById(R.id.profil_picture);
                Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.me);
                RoundImage roundedImage = new RoundImage(img);
                profilPicture.setImageDrawable(roundedImage);
                */
            }
        });

        // ACTION BOUTON AJOUTER CONTACT
        addContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                FragmentAddContact fragment2 = new FragmentAddContact();
                transaction.add(R.id.myFrameLayout, fragment2);
                transaction.addToBackStack("FragmentAddContact");
                transaction.commit();
            }
        });
        return myView;
    }

    // EVENT IMAGE SELECTED
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMG && resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;//returning null for below statement
            Bitmap img = BitmapFactory.decodeFile(selectedImage.toString(), options);

            RoundImage roundedImage = new RoundImage(img);
            profilPicture.setImageDrawable(roundedImage);
        }
    }
}

有RoundImage类的代码(它是一个在imageView中生成裁剪圆圈图像的类):

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

public class RoundImage extends Drawable {
    private final Bitmap mBitmap;
    private final Paint mPaint;
    private final RectF mRectF;
    private final int mBitmapWidth;
    private final int mBitmapHeight;

    public RoundImage(Bitmap bitmap) {
        mBitmap = bitmap;
        mRectF = new RectF();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);

        mBitmapWidth = mBitmap.getWidth();
        mBitmapHeight = mBitmap.getHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawOval(mRectF, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRectF.set(bounds);
    }

    @Override
    public void setAlpha(int alpha) {
        if (mPaint.getAlpha() != alpha) {
            mPaint.setAlpha(alpha);
            invalidateSelf();
        }
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmapWidth;
    }

    @Override
    public int getIntrinsicHeight() {
        return mBitmapHeight;
    }

    public void setAntiAlias(boolean aa) {
        mPaint.setAntiAlias(aa);
        invalidateSelf();
    }

    @Override
    public void setFilterBitmap(boolean filter) {
        mPaint.setFilterBitmap(filter);
        invalidateSelf();
    }

    @Override
    public void setDither(boolean dither) {
        mPaint.setDither(dither);
        invalidateSelf();
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

}

最后,还有代码xml来创建imageview:

<ImageView
        android:id="@+id/profil_picture"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:scaleType="centerCrop"/>

1 个答案:

答案 0 :(得分:0)

试试这可能对你有帮助。

int id3 = getResources().getIdentifier("com.example.admin:mipmap/status" , null, null);

((ImageView) findViewById(R.id.imageviewstatus)).setImageResource(id3);