图像视图中的Android图像未显示

时间:2013-01-22 05:09:31

标签: android android-imageview

我正在尝试使用相机意图拍摄照片,将图像保存在SD卡中,通过按钮单击方法保存共享首选项中的文件路径 在另一个活动中显示相同的图像,但图像不会显示。

这是我的代码

拍摄照片布局:

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/remove1"
            style="@style/SmallButtonText"
            android:background="@drawable/loginbutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_remove" />

        <Button
            android:id="@+id/take1"
            style="@style/SmallButtonText"
            android:background="@drawable/loginbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_take" />
    </LinearLayout>
</RelativeLayout>

拍照码:

Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageName1 = "image"+String.valueOf(System.currentTimeMillis()) + ".jpg"; 
File file = new File(Environment.getExternalStorageDirectory(),"/new/images/"+     imageName1);
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri1);
imageUri1 = Uri.fromFile(file)       
startActivityForResult(intent1, IMAGE_CAPTURE);

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if(requestCode == IMAGE_CAPTURE)
        {
            Bundle b = data.getExtras(); 
            Bitmap pic = (Bitmap)b.get("data");
            if (pic != null) {
                pictureHolder1.setImageBitmap(pic);
            }
        }
    }
}

onbtnclick()
{
    SharedPreferences image1Pref = getSharedPreferences("images",MODE_PRIVATE);
    Editor et=image1Pref.edit();
    et.putString("image1",imageName1);
    et.commit();
}

anotheractivitylayout:

<ImageView
 android:id="@+id/pic1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:src="@drawable/ic_launcher" />

活动代码:

SharedPreferences image1Pref = getSharedPreferences("images",MODE_PRIVATE);
String picName=image1Pref.getString("image1","");
File piFile1 =new File(Environment.getExternalStorageDirectory(),"/new/images/"+picName);

if(piFile1!=null)
{
    Bitmap bitmap1 = BitmapFactory.decodeFile(piFile1.getAbsolutePath());
    pic1.setImageBitmap(bitmap1);
}

1 个答案:

答案 0 :(得分:0)

只需将图像设置如下:

 if(piFile1!=null)
  {
      Bitmap bitmap1 = BitmapFactory.decodeFile(piFile1.getPath()); <<<Change this line.
      pic1.setImageBitmap(bitmap1);
  }