保存图片并加载到imageview中

时间:2014-02-23 22:14:44

标签: android image load imageview save

如果我拍照,它会在图像视图中显示。这很有效。但是如何将照片另外保存在硬盘上呢?拿他相机设置的大小?

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);

    }}
}

我事先感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

制作uri并提交

public File mediaFile;
public Uri fileUri;

将这两种方法放在你的文件中

/** 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(intent,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"/>