我正在嘗試從MainActivity
的默認相機意圖中拍攝一張照片,然後將該圖像放入同一活動的ImageView
中。嘗試顯示從相機意圖拍攝的圖像
我保存圖像,使得拍攝的圖像將覆蓋以前拍攝的圖像(在這種情況下,我所說的圖像test.jpg
,我將它存儲在SD卡)
我現在有我的代碼的問題是ImageView
顯示上次應用程序運行時拍攝的照片。
這是我的代碼。
public class MainActivity extends Activity
{
ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imv = (ImageView)findViewById(R.id.imv);
Uri uri = null;
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.jpg";
try
{
uri = takePhoto(path);
}
catch(Exception e)
{
e.printStackTrace();
}
imv.setImageURI(uri);
}
private Uri takePhoto(String path) throws IOException
{
File photo = new File(path);
photo.createNewFile();
Uri fileUri = Uri.fromFile(photo);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, 0);
fileUri = Uri.fromFile(new File(path));
return fileUri;
}
}
http://chintankhetiya.wordpress.com/2013/12/14/picture-selection-from -camera-gallery/ –
你的'onActivityResult'方法在哪裏? – GrIsHu
看看我的答案。 – GrIsHu