怎么看摄像头的应用程序拍照异步的画廊 [英] How to see camera app take photo asynchronous in the gallery

查看:132
本文介绍了怎么看摄像头的应用程序拍照异步的画廊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个基本的相机应用。我可以拍照,我可以看到它在我的应用程序。但我想看看我的照片库异步的。当重新启动手机,我可以看到我的照片在画廊。

样品code。

 公共类PhotosActivity扩展片段实现View.OnClickListener {

    私人最终诠释REQUEST_ code = 100;

    私人按钮fotobutton;
    私人ImageView的foto_image;
    私有静态最后弦乐IMAGE_DIRECTORY_NAME =OlkunMustafa;
    公共静态最终诠释MEDIA_TYPE_IMAGE = 1;

    私人乌里了fileURI;
    //目录名来存储拍摄的照片和视频

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

        查看rootView = inflater.inflate(R.layout.photos_activity,集装箱,假);
        fotobutton =(按钮)rootView.findViewById(R.id.fotobutton);
        foto_image =(ImageView的)rootView.findViewById(R.id.foto_image);

        fotobutton.setOnClickListener(本);

        返回rootView;
    }

    @覆盖
    公共无效的onClick(视图v){
        如果(V == fotobutton){
            意图photo_intent =新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);

            =了fileURI getOutputMediaFile(MEDIA_TYPE_IMAGE);
            photo_intent.putExtra(MediaStore.EXTRA_OUTPUT,了fileURI);

            startActivityForResult(photo_intent,100);
        }
    }

    @覆盖
    公共无效onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,因此code,数据);
        如果(结果code == getActivity()。RESULT_OK)
        {
            BitmapFactory.Options选项=新BitmapFactory.Options();
            最后的位图位= BitmapFactory.de codeFILE(fileUri.getPath()
                    选项​​);
            foto_image.setImageBitmap(位);

            意图mediaScanIntent =新的意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            乌里contentUri = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            mediaScanIntent.setData(contentUri);
            。getActivity()sendBroadcast(mediaScanIntent);
        }
    }

    私有静态乌里getOutputMediaFile(整型){

        //外部SD卡的位置
        文件mediaStorageDir =新的文件(
                环境
                        .getExternalStorageDirectory(),
                IMAGE_DIRECTORY_NAME);

        //创建存储目录,如果它不存在
        如果(!mediaStorageDir.exists()){
            如果(!mediaStorageDir.mkdirs()){
                Log.d(IMAGE_DIRECTORY_NAME,哎呀!创建失败
                        + IMAGE_DIRECTORY_NAME +目录);
                返回null;
            }
        }

        //创建一个媒体文件名
        字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS
                。Locale.getDefault())格式(新的日期());
        文件媒体文件;
        如果(类型== MEDIA_TYPE_IMAGE){
            媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符
                    +IMG_+的timeStamp +.JPG);
        }
        其他 {
            返回null;
        }

        返回Uri.fromFile(媒体文件);
    }
}
 

我该怎么办呢?

解决方案
  

您需要调用广播告诉Android操作系统已经添加图像

第一种方法

 私人无效galleryAddPic()
    {
       如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.KITKAT)
       {
        意图mediaScanIntent =新的意图(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        文件F =新的文件(文件://+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        乌里contentUri = Uri.fromFile(F);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
       }
       其他
       {
        sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStorageDirectory())));
       }
    }
 

第二法

,或致电

  ctx.sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
 

也对这个答案看看 在此输入链接的描述

您可以使用此方法来存储介质存储提供的图像文件:

 公共静态无效addImageToGallery(最后字符串文件路径,最终的上下文语境){

    ContentValues​​值=新ContentValues​​();

    values​​.put(Images.Media.DATE_TAKEN,System.currentTimeMillis的());
    values​​.put(Images.Media.MIME_TYPE,为image / jpeg);
    values​​.put(MediaStore.MediaColumns.DATA,文件路径);

    。context.getContentResolver()插入(Images.Media.EXTERNAL_CONTENT_URI,价值观);
}
 

并ScanListener

您可以直接保存图像库

  MediaStore.Images.Media.insertImage(getContentResolver(),yourBitmap,yourTitle,yourDescription);
 

I am working on a basic camera app. I can take a photo, and I can see it in my app. But I want to see my photo in gallery asynchronous. When restart the phone, I can see my photo in the gallery.

Sample code.

public class PhotosActivity extends Fragment implements View.OnClickListener {

    private final int REQUEST_CODE   = 100;

    private Button fotobutton;
    private ImageView foto_image;
    private static final String IMAGE_DIRECTORY_NAME = "OlkunMustafa";
    public static final int MEDIA_TYPE_IMAGE = 1;

    private Uri fileUri;
    // Directory name to store captured images and videos

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView       = inflater.inflate(R.layout.photos_activity,container,false);
        fotobutton          = (Button) rootView.findViewById(R.id.fotobutton);
        foto_image          = (ImageView) rootView.findViewById(R.id.foto_image);

        fotobutton.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        if( v == fotobutton) {
            Intent photo_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            fileUri = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            photo_intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            startActivityForResult(photo_intent,100);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == getActivity().RESULT_OK)
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);
            foto_image.setImageBitmap(bitmap);

            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            mediaScanIntent.setData(contentUri);
            getActivity().sendBroadcast(mediaScanIntent);
        }
    }

    private static Uri getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStorageDirectory(),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } 
        else {
            return null;
        }

        return Uri.fromFile(mediaFile);
    }
}

How can I do it?

解决方案

You need to call a broadcast to tell Android OS that you have added an Image

First Method

    private void galleryAddPic() 
    {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
       {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
       }
       else
       {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
       }
    }

Second Method

Or Call

ctx.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));

Also have a look on this Answer enter link description here

you can use this method to store image file on media store provider:

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

And ScanListener

You can directly save image to Gallery

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

这篇关于怎么看摄像头的应用程序拍照异步的画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆