2012-10-03 50 views
0

我已經創建了使用內置圖像捕捉活動來拍照的活動,但仍然無法將圖片存儲在SD卡中並查看捕獲的圖像。意向已啓動,我可以拍照,但當我點擊在確定(保存),沒有任何事情發生。下面是我的活動:Android圖片活動

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.picturelayout); 
    imageForUpload = (ImageView) findViewById(R.id.trackMePicture); 
    btnBack = (Button) findViewById(R.id.btnBack); 
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(this)); 
    startActivityForResult(intent, TAKE_PHOTO_CODE); 
} 

/** 
* @return 
*/ 
private Uri getImageUri(Context context) { 
    // TODO Auto-generated method stub 

    File file =newFile(Environment.getExternalStorageDirectory(),context.getPackageName()); 
    if(!file.exists()) 
     file.mkdir(); 
    File newFile=new File(file,new Date().toString()+".jpg"); 
    Uri imagePath=Uri.fromFile(newFile); 
    return imagePath; 

} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode==TAKE_PHOTO_CODE){ 
     if(resultCode==-1){ 

      Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show(); 
      //Uri imagePath=getImageUri(); 
      Bitmap b; 
      try { 
       b = Media.getBitmap(getContentResolver(), getImageUri(this)); 
       imageForUpload.setImageBitmap(b); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }else{ 
      Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show(); 
     } 
    } 
    else{ 
     Toast.makeText(getApplicationContext(), "Request code : "+requestCode, Toast.LENGTH_LONG).show(); 
    } 
} 
+0

請使用搜索功能。關於如何將相機中的圖像保存到SD卡上,大約有600萬個問題。只要看看右邊的「相關」列表--->謝謝 – Simon

回答

0

。在你沒有onActivityResult()代碼,將位圖b存儲在一個文件中。 你可以看看我的Zwitscher應用程序中的PicHelper.storeBitmap()方法,瞭解如何做到這一點。

0

試着這樣做:

final Bundle extras = data.getExtras(); 
if (extras != null) { 
    Bitmap b = extras.getParcelable("data"); 
    imageForUpload.setImageBitmap(b); 

} 
0

這裏是我如何做到這一點(用於拍照或瀏覽媒體目錄),希望它能夠幫助:

static int SELECT_IMAGE = 2000; 
static int CAMERA_REQUEST = 2001; 

public void ProfilePictureDialog() 
{ 
    builder = new AlertDialog.Builder(this); 
    builder.setCancelable(false); 
    builder.setMessage("").setPositiveButton("Browse images", new DialogInterface.OnClickListener() 
    { 
     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 
      Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);  
      startActivityForResult(gallery, SELECT_IMAGE); 

     }}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
     { 
     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 

     }}).setNeutralButton("Take picture with camera", new DialogInterface.OnClickListener(){ 
     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     }}); 
    builder.show(); 
} 





protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if(resultCode == RESULT_OK) 
    { 
     if (requestCode == CAMERA_REQUEST) 
     { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      ImageView profilePicture = (ImageView) findViewById(R.id.profilepic); 

      photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true); 
      profilePicture.setImageBitmap(photo); 
     } 
     else if(requestCode == SELECT_IMAGE) 
     { 
      Uri selectedImagePath = data.getData(); 

      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImagePath, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 

      Bitmap photo = BitmapFactory.decodeFile(filePath); 

      ImageView profilePicture = (ImageView) findViewById(R.id.profilepic); 

      photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true); 
      profilePicture.setImageBitmap(photo); 
     } 
    } 
}