如何获取已保存到图库的图片的URI

时间:2016-11-02 02:12:01

标签: android uri mediastore

我有一个小相机片段,目前会拍摄一些照片,然后将其保存到相机库。我们的想法是,从现在开始,每次加载此片段时,都应检查是否存在应该加载的Image,我打算通过将URI的字符串版本保存到首选项中的文件来实现。我目前正在保存原始文件的URI,但这似乎不起作用。我的代码在下面

public class StudentID extends Fragment {
    String mCurrentPhotoPath;
    Uri mPhotoURI;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final int REQUEST_TAKE_PHOTO = 1;
    ImageView picView;
    FloatingActionButton button;
    public StudentID() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment StudentID.
     */
    // TODO: Rename and change types and number of parameters
    public static StudentID newInstance() {
        StudentID fragment = new StudentID();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_student_id, container, false);
        picView = (ImageView)view.findViewById(R.id.idPic);
        picView.setScaleType(ImageView.ScaleType.FIT_XY);
        button = (FloatingActionButton)view.findViewById(R.id.picButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takePicture();
            }
        });
        updateImage();
        return view;
    }
    private void updateImage(){
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        mPhotoURI = Uri.parse(sharedPref.getString("StudentID", ""));
        picView.setImageURI(mPhotoURI);
    }
    private void takePicture() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
            }
            if (photoFile != null) {
                Uri photoURI = Uri.fromFile(photoFile);
                mPhotoURI = photoURI;
                mCurrentPhotoPath = photoFile.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) {
            picView.setImageURI(mPhotoURI);
            addImageToGallery(mCurrentPhotoPath,getContext());
        }
    }
    public static void addImageToGallery(final String filePath, final Context context) {

        ContentValues values = new ContentValues();

        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.MediaColumns.DATA, filePath);
        context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
    }
    private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName, 
                ".jpg",        
                storageDir    
        );
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }
}

0 个答案:

没有答案