2012-10-20 42 views
1

我有一個活動,調用相機意圖捕獲圖片並將其指定爲個人資料圖片。該活動工作正常,除了當我點擊回來,然後再次打開活動時,圖片不再顯示。當活動再次打開時顯示捕獲的圖片

每當用戶打開此活動時,如何顯示它?這是我該活動

public class MyAccountActivity extends Activity { 

private static final int CAMERA_REQUEST = 1888; 
private TextView name; 
private TextView userId; 
private TextView address; 
private TextView email; 
private TextView phone; 
private ImageButton profilePicture; 
private Bitmap bm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_account); 
    setUpViews(); 
Log.v("test","this is test: "+LoginActivity.user.getName()); 
} 

private void setUpViews() { 
    //setting up views 

    //calling user details from User [] instance 

} 

public void ViewPicture(View v) { 
    Intent intent = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath()); 
      startActivityForResult(intent, CAMERA_REQUEST); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
     super.onActivityResult(requestCode, resultCode, data); 
       bm = (Bitmap) data.getExtras().get("data"); 
       profilePicture.setImageBitmap(bm); 
       MediaStore.Images.Media.insertImage(getContentResolver(), bm, null, null); 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object 
       byte[] b = baos.toByteArray(); 
} 

我試圖打電話給profilePicture.setImageBitmap(bm)onResume()但我的應用程序崩潰的代碼。 任何幫助,非常感謝。

+1

嘗試從調用的onResume setupViews。您在創建活動時設置了所有內容,但當您回到活動時不會重新創建活動,因此我不認爲該圖像正在實施。當您嘗試設置圖像時會發生什麼樣的崩潰? – toadzky

+0

感謝您的快速回復...虐待測試您的建議,生病發布迴應。問候 –

+0

@toadzky它沒有改變任何東西。我認爲,因爲我從意圖獲得結果時設置了profilePicture。我在想如果我可以從手機的保存位置加載圖片?那可能嗎?再次感謝 –

回答

0

保存一些URI路徑

Uri uri = Uri.withAppendedPath(Uri.fromFile(context.getExternalFilesDir(null)), imageName.png); 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri)); 

所拍攝的圖像,然後當你要顯示的圖像,使用該URI

profilePicture.setImageUri(uri) 
+0

上述代碼在調用攝像機意圖時正確嗎?所以基本上在我的ViewPicture()? –

+0

似乎這個建議是正確的解決方案,但不幸的是我無法正確實施它。 Registers –

+0

[link](http://stackoverflow.com/questions/5661424/how-to-capture-the-image-and-save-it-in-sd-card)另一個解決方案。 –

相關問題