将位图放到ImageView

时间:2014-04-17 09:04:45

标签: android bitmap android-imageview

我正在开展活动,将相机拍摄的照片放到较小的ImageView字段中。几乎所有代码都来自互联网,我正在尝试将该代码放在我的活动中。所以不要真的知道这一行会发生什么:**mImageView.setImageBitmap(bitmap);**。 logcat显示:

04-17 11:55:47.791: E/AndroidRuntime(4272): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.project.simplify/com.project.simplify.PhotoUploadActivity}: java.lang.NullPointerException

我的方法在这里:

private void setPic() {


        /* There isn't enough memory to open up more than a couple camera photos */
        /* So pre-scale the target bitmap into which the file is decoded */
        /* reviewLayout */
        reviewLayout = new LinearLayout(mcontext);
        reviewLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// cascas
        reviewLayout.setOrientation(LinearLayout.VERTICAL);
        reviewLayout.setGravity(Gravity.CENTER_VERTICAL);
        reviewLayout.setBackground(getResources().getDrawable(
                R.drawable.menu_button_bg));

        /* reviewEntryTopLayout */
         reviewEntryTopLayout = new LinearLayout(this);
        reviewEntryTopLayout
                .setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        densityToPixels(90)));
        reviewEntryTopLayout.setOrientation(LinearLayout.HORIZONTAL);
        reviewEntryTopLayout.setGravity(Gravity.CENTER_VERTICAL);

        /* reviewsLayout */
         reviewsLayout = (LinearLayout) findViewById(R.id.photoslayout);
        reviewsLayout.addView(reviewLayout);



        /* image   (entry number)*/
        /* Get the size of the ImageView */
        int targetW = 90;   //cc
        int targetH = 120;

        /* Get the size of the image */
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        /* Figure out which way needs to be reduced less */
        int scaleFactor = 1;
        if ((targetW > 0) || (targetH > 0)) {
            scaleFactor = Math.min(photoW/targetW, photoH/targetH); 
        }

        /* Set bitmap options to scale the image decode target */
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        /* Decode the JPEG file into a Bitmap */
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

        /* Associate the Bitmap to the ImageView */
        **mImageView.setImageBitmap(bitmap);**
        mImageView.setVisibility(View.VISIBLE);
         // sutvarkyt photo i maza
        reviewLayout.addView(reviewEntryTopLayout);
        reviewEntryTopLayout.addView(mImageView);
        /////////////////////////////////////////////////
        /* table layout */

        TableLayout tl = new TableLayout(this);
        tl.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        tl.setPadding(densityToPixels(20), 0, 0, 0);
        reviewEntryTopLayout.addView(tl);

        /* tableRow1 w */
        TableRow tableRow1 = new TableRow(this);
        tableRow1.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        tl.addView(tableRow1);

        /* textView1 w */
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        textView1.setText("nuotraukos datas ");

        tableRow1.addView(textView1);
    }

尝试进入这种布局:

  <LinearLayout
            android:id="@+id/photoslayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lightSeperator"
            android:orientation="vertical" >

1 个答案:

答案 0 :(得分:0)

在xml文件中执行以下操作:

<LinearLayout
        android:id="@+id/photoslayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lightSeperator"
        android:orientation="vertical" >
 <ImageView
       android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
  />
</LinearLayout>

并替换您获得异常的代码,如下所示

  ImageView mImageView = (ImageView) findViewById(R.id.imageView);
  mImageView.setImageBitmap(bitmap);

我希望这会有所帮助。