我正在尝试使用相机意图拍照,将图像保存在 sdcard 中,将文件路径保存在按钮单击方法的共享首选项中,并在另一个活动中显示相同的图像,但图像不显示。
这是我的代码
拍照布局:
<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();
}
另一个活动布局:
<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);
}