2011-12-19 55 views
1

我有一個按鈕和imageview查看。在那裏,我想用android相機拍攝一張大尺寸的照片,並將其設置在imageview中。使用下面的代碼,我在imageview中沒有獲得圖像。什麼是錯誤的,我的代碼嗎?請help.Thank你幫忙相機照片圖像全尺寸圖像查看

package de.camera.test; 
... 

public class CameratestActivity extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 

public File mTemp; 
public ImageView image; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button cameraButton = (Button) findViewById(R.id.button); 
    image = (ImageView) findViewById(R.id.imageview); 
    cameraButton.setOnClickListener(this); 
} 

public void onClick(View v) { 

    Intent camera = new Intent(
      android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    try { 
     mTemp = File.createTempFile("myCameraShot", null); 
     camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
       Uri.fromFile(mTemp)); 
     startActivityForResult(camera, 0); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (data == null) 
     return; 
    Bitmap b; 
    try { 
     b = Media.getBitmap(getContentResolver(), Uri.fromFile(mTemp)); 

     image.setImageBitmap(b); 
     Drawable d = image.getDrawable(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

佈局的main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/preview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 


</LinearLayout> 

回答

0

File.createTempFile("myCameraShot", null)的文檔中說,一個臨時文件保存在臨時文件的默認位置創建。這個文件很可能受到保護,只有您的應用可以訪問它。

我想你可以嘗試的是指定一個SD卡上的文件路徑。我也建議你看看這個:Creating temporary files in Android