1
我開發一個Android應用程序,我希望我的應用程序能夠拍照並顯示它們。 我的問題是,它使圖片,存儲在SD卡上,但沒有顯示在我的應用程序,ImageView保持空白。 繼承人我的代碼:安卓相機(照片未顯示)
public class OfferCreation2 extends ActionBarActivity {
private Uri imageUri;
private static int TAKE_PICTURE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offer_creation2);
Button buttonTakePhoto = (Button) findViewById(R.id.buttonTakePhoto);
buttonTakePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto(v);
}
});
}
private void takePhoto(View v){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode == Activity.RESULT_OK){
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView picture = (ImageView) findViewById(R.id.imageProduct);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try{
bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
picture.setImageBitmap(bitmap);
Toast.makeText(OfferCreation2.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(OfferCreation2.this, "FAIL?", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_offer_creation2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}