2014-02-23 73 views
0

如果我拍摄一张照片,它将显示在图像视图中。但是,我怎样才能将照片另存到硬盘上?拿他的相机设置的大小?保存图片并加载到图像视图中

public class Note extends Activity { 

TextView t; 
ImageView iv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.note); 
    iv=(ImageView) findViewById(R.id.imageView); 

    Button btn = (Button) findViewById(R.id.photo); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, 0); 
     } 
    }); 


} 
public void button (View view){ 
    Intent intent = new Intent(view.getContext(),Note2.class); 
    startActivityForResult(intent, 0); 
} 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode==0) 
    { 
    Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 

    }} 
} 

我事先感谢您的帮助。

回答

0

化妆URI和文件

public File mediaFile; 
public Uri fileUri; 

把这2种方法在你的文件

/** Create a file Uri for saving an image or video */ 
    private static Uri getOutputMediaFileUri(int type){ 
      return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** Create a File for saving an image or video */ 
    private static File getOutputMediaFile(int type){ 
     // To be safe, you should check that the SDCard is mounted 
     // using Environment.getExternalStorageState() before doing this. 

     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "YourFolderName"); 
     // This location works best if you want the created images to be shared 
     // between applications and persist after your app has been uninstalled. 

     // Create the storage directory if it does not exist 
     if (! mediaStorageDir.exists()){ 
      if (! mediaStorageDir.mkdirs()){ 
       Toast.makeText(null, "failed to create directory", Toast.LENGTH_SHORT).show(); 
       return null; 
      } 
     } 




     if (type == 1){ 
       Long tsLong = System.currentTimeMillis()/1000; 
       String ts = tsLong.toString(); 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
      "photo"+ ts + ".jpg"); 

     } 
     else { 
      return null; 
     } 

     return mediaFile; 
    } 

那么你之前startActivityForResult(意向,0);放:

fileUri = getOutputMediaFileUri(1); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
      intent.putExtra("return-data", true); 

确保在您的清单,你把:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>