【问题标题】:Putting the Bitmap to ImageView将位图放到 ImageView
【发布时间】:2014-04-17 09:04:44
【问题描述】:

我正在进行活动,将相机拍摄的照片放到较小的 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" >

【问题讨论】:

  • mImageView.setImageBitmap(bitmap); 处下断点,看看 bitmap 是否为空。
  • 如果它是空的?因为它是
  • 那说明加载失败..所以,检查路径是否正确,或者检查图像是否真的是bmp

标签: android bitmap android-imageview


【解决方案1】:

在您的 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);

我希望这会有所帮助。

【讨论】:

  • 他说他已经这样做了,所以这对他来说不是正确的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
相关资源
最近更新 更多