即使对于特定路径,位图也会产生null

时间:2014-01-02 11:06:38

标签: android bitmap android-adapterview

我正致力于从相机和画廊获取图像。我正在尝试获取图像的位图以减小图像的大小。但是当我尝试调试进程时,bitmapfactory为null。我不明白为什么即使对于绝对的uri也是如此。我正在实现内部适配器。

@SuppressLint("ResourceAsColor")
public class PSAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<Message> mMessages;



    public PSAdapter(Context context, ArrayList<Message> messages) {
        super();
        this.mContext = context;
        this.mMessages = messages;
    }
    @Override
    public int getCount() {
        return mMessages.size();
    }
    @Override
    public Object getItem(int position) {       
        return mMessages.get(position);
    }
    @SuppressWarnings("deprecation")
    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message message = (Message) this.getItem(position);

        ViewHolder holder; 
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.sms, parent, false);
            holder.message = (TextView) convertView.findViewById(R.id.message_text);
            holder.picture = (ImageView) convertView.findViewById(R.id.chat_image);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();
        if(message.message==null){
            holder.message.setVisibility(View.GONE);
            holder.picture.setVisibility(View.VISIBLE);
            Bitmap attachment = getScaledBitmap("content://media/external/images/media/4391", 800, 800);
            holder.picture.setImageBitmap(attachment);
        }else{
            holder.message.setVisibility(View.VISIBLE);
            holder.picture.setVisibility(View.GONE);
            holder.message.setText(message.getMessage());
        }
        LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();

        if(message.isStatusMessage())
        {
            holder.message.setBackgroundDrawable(null);
            lp.gravity = Gravity.LEFT;
            holder.message.setTextColor(R.color.textFieldColor);
        }
        else
        {       

            if(message.isMine())
            {
                holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
                lp.gravity = Gravity.RIGHT;
            }

            else
            {
                holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
                lp.gravity = Gravity.LEFT;
            }
            holder.message.setLayoutParams(lp);
            holder.message.setTextColor(R.color.textColor); 
        }
        return convertView;
    }
    public static class ViewHolder
    {
        TextView message;
        ImageView picture;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    private Bitmap getScaledBitmap(String picPath, int width, int height){
        BitmapFactory.Options sizeOptions = new BitmapFactory.Options();
        sizeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(picPath,sizeOptions);

        int inSampleSize = imageSize(sizeOptions, width, height);

        sizeOptions.inJustDecodeBounds = false;
        sizeOptions.inSampleSize = inSampleSize;
        return BitmapFactory.decodeFile(picPath, sizeOptions);
    }

    private int imageSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
        final int height = options.outHeight;
        final int width = options.outWidth;
        int size = 1;

        if(height>reqHeight || width>reqWidth){
            final int heightRatio = Math.round((float)height/(float)reqHeight);
            final int widthRatio = Math.round((float)width/(float)reqWidth);


            size = heightRatio<widthRatio? heightRatio:widthRatio;
        }
        return size;
    }
}

0 个答案:

没有答案