先上代码:
package com.create.intenttest; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; public class MainActivity extends AppCompatActivity { private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp = getSharedPreferences("config", MODE_PRIVATE); Button takePhoto = (Button) findViewById(R.id.btn); Button selectPhoto = (Button) findViewById(R.id.btn2); takePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用相机逻辑 Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 下面这句指定调用相机拍照后的照片存储的路径 //注意,当给照片指定路径后,onactivityresult()方法中返回的data就为null了,这时要相取的数据 //可以考虑把你设定的路径存到sp中,设置照片时在sp中取出来,使用imageView.setImageBitmap(sp.getString("photoPath")) //当然,如果你拍摄和获取是在同一个activity中路径直接设为成员变量就好。无需sp。 // takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, // Uri.fromFile(new File(Environment.getExternalStorageDirectory(), Constant.PHOTONAME))); // sp.edit().putString(Constant.TAKE_PHOTO_PATH, Environment.getExternalStorageDirectory() + "/" + Constant.PHOTONAME).commit(); startActivityForResult(takeIntent, Constant.TAKEPHOTO_CODE); } }); selectPhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/jpeg"); startActivityForResult(intent, Constant.SELECT_PIC); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e("data", data + "----------------"); ImageView imageView = (ImageView) findViewById(R.id.image); switch (requestCode) { case Constant.TAKEPHOTO_CODE://相机返回 if (data != null) { if (data.hasExtra("data")) { Bitmap thumbnail = data.getParcelableExtra("data"); imageView.setImageBitmap(thumbnail); } } case Constant.SELECT_PIC://相册返回 if (data != null) { Uri uri = data.getData(); imageView.setImageURI(uri); } } } }
以下博文作者讲的比较透彻,不过里面onactivityresult()方法中的获取代码,中的注释貌似有点问题:
http://www.cnblogs.com/mengdd/archive/2013/03/31/2991932.html
// 指定了存储路径的时候(intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);)
// Image captured and saved to fileUri specified in the
// Intent
Toast.makeText(this, "Image saved to:
" + data.getData(), Toast.LENGTH_LONG).show();
不明白作者以上代码要放在if(data!=null) 因为data不等于null,说明打开相机时没有指定路径,所以可能是作者的误操作。应该放入else{}中
if (data != null) { // 没有指定特定存储路径的时候 Log.d(LOG_TAG, "data is NOT null, file on default position."); // 指定了存储路径的时候(intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);) // Image captured and saved to fileUri specified in the // Intent Toast.makeText(this, "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show(); if (data.hasExtra("data")) { Bitmap thumbnail = data.getParcelableExtra("data"); imageView.setImageBitmap(thumbnail); } } else { Log.d(LOG_TAG, "data IS null, file saved on target position."); // If there is no thumbnail image data, the image // will have been stored in the target output URI.
以下博文中讲了拍照后怎么把真实大小的照片获取到,并存放到指定的路径。注意是真实大小,因为直接在onactivityresult中获取的bitmap是缩略图。不清晰
http://blog.csdn.net/ryantang03/article/details/8654137