-1

SDカードから画像を取得してギャラリービューに配置したい.そのために、その画像をビットマップに変換しましたが、特定のエラーが表示されます..

画像を変換するためのコード..ヌルポインター例外を示す完全なコードを添付しています

  private Gallery gallery;
private ImageView imgView;
int position;
private byte[] data = { };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);

    System.out.println("Enter the activity");

    try{
        String filepath = "/sdcard/";
        File imagefile = new File(filepath + "abcdoutput.jpg");
        FileInputStream fis = new FileInputStream(imagefile);
        Bitmap bi = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();
        System.out.println("cnvrtn");

    }
    catch(Exception e) {
         e.printStackTrace();

    }

    Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
    position = 0;
    imgView = (ImageView) findViewById(R.id.ImageView01);
    imgView.setImageBitmap(bitmapimage);

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            imgView.setImageResource(data[position]);
            DisplayImage.this.position = position;
        }
    });
    System.out.println("Enter the activity//////////");

    imgView.setOnLongClickListener(new View.OnLongClickListener() {
        @SuppressWarnings("deprecation")
        public boolean onLongClick(View v) {

            AlertDialog alertDialog = new AlertDialog.Builder(
                    DisplayImage.this).create();
            alertDialog.setTitle("Confirmation");
            alertDialog
                    .setMessage("Do you want to set this image as wallaper?");
            alertDialog.setButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {

                            Bitmap bitmap = BitmapFactory.decodeResource(
                                    getResources(), data[position]);
                            try {
                                DisplayImage.this.setWallpaper(bitmap);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            Log.d("Gallery Example", "Image setted.");

                        }
                    });

            alertDialog.show();
            return true;
        }
    });

}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = null;
        GalItemBg = typArray.getResourceId(
                0, 0);
        typArray.recycle();
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgView = new ImageView(cont);

        imgView.setImageResource(data[position]);
        imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}
4

2 に答える 2

0

これを使用して、配列から画像を取得して設定します。

Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);  

imgView.setImageBitmap(bitmap);
于 2013-01-07T08:46:17.787 に答える
0

試してみて、1行のコードで簡単に

String imagePath=Environment.getExternalStorageDirectory().toString()+"/abcdoutput.jpg";
imgView.setImageDrawable(Drawable.createFromPath("imagePath"));
于 2013-01-07T08:47:05.617 に答える