使用MediaMetadataRetriever捕获图像并将其保存到使用位图的设备中

时间:2019-04-07 10:51:26

标签: java android image bitmap storage

我正在尝试通过让用户拍摄imageView的图片并在没有保存按钮的情况下将其保存而将图像位图保存到将要运行该应用程序的设备的存储中,但是我无法保存该图像,我花了最后几天看在线示例,我似乎无法解决问题。 android studio在运行该应用程序时出现了新问题,但图像仍不会保存到设备中。

//这是获取图像的代码

View.OnClickListener btnCaptureOnClickListener = new View.OnClickListener(){
    @Override
    public void onClick(View v){



        int currentPosition = video.getCurrentPosition(); //in millisecond
        Toast.makeText(VideoViewer.this,
                "Current Position: " + currentPosition + " (ms)",
                Toast.LENGTH_LONG).show();

        // capture the image at current postion
        Bitmap bmFrame = 
myMediaMetadataRetriever.getFrameAtTime(currentPosition * 1000);

enter code here

        if(bmFrame == null){
            Toast.makeText(VideoViewer.this,
                    "bmFrame == null!",
                    Toast.LENGTH_LONG).show();
        }else {

            AlertDialog.Builder myCaptureDialog =
                    new AlertDialog.Builder(VideoViewer.this);
            ImageView capturedImageView = new ImageView(VideoViewer.this);
            capturedImageView.setImageBitmap(bmFrame);
            LinearLayout.LayoutParams capturedImageViewLayoutParams =
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);


capturedImageView.setLayoutParams(capturedImageViewLayoutParams);

//这是将文件保存到我的设备的代码

            String root = 
Environment.getExternalStorageDirectory().getAbsolutePath();
            File MyDir = new File(root +"/save_images");
            MyDir.mkdirs();

            String fname = "Image-"+ O +".jpg";
            File file = new File (MyDir, fname);
            if (file.exists()) file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bmFrame.compress(Bitmap.CompressFormat.JPEG,90, out);
                out.flush();
                out.close();
                Toast.makeText(VideoViewer.this, "saved picture", 
Toast.LENGTH_SHORT).show();

            }catch(Exception e ){
                e.printStackTrace();
            }
            myCaptureDialog.setView(capturedImageView);
            myCaptureDialog.show();

        }

    }

0 个答案:

没有答案