有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android映像未存储在存储器中

我试图通过相机将新拍摄的图像存储在存储器中,然后将图像显示在imageview上。但是,图像不会被存储

我的代码:

private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

......more code...

captureImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(null, null, new PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {

                        File pictureFile;
                        pictureFile = new File(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));


                        try {
                            FileOutputStream fos = new FileOutputStream(pictureFile);
                            fos.write(data);
                            fos.close();
                        }
                        catch (Exception e) {
                            Log.d("ERR",e.toString());
                        }

                        //Display
                        Uri uri = Uri.fromFile(pictureFile);
                        imageViewer.setImageURI(uri);

                     }
                });
            }
        });

我的代码写和显示图像正确吗?我在存储器上看不到任何图像,imageview也不会被图像填充。我做错了什么


共 (3) 个答案

  1. # 1 楼答案

    创建要保存照片的文件夹。 指定创建文件的完整路径名

    //在本例中MyImages是一个文件夹,您可以在其中保存照片

     File storageDir = new File(Environment.getExternalStorageDirectory() + "/", "MyImages");
    
                if (!storageDir.exists()) {
                    storageDir.mkdirs();//directory created
                }
                // get the current timestamp
                String timest = new SimpleDateFormat("yyyyMMdd_HHmmss")
                        .format(new Date());
    //Create your picture file
     File pictureFile;
     pictureFile = new File(storageDir.getPath()+File.separator+ "IMG_" + timeStamp + ".jpg");
    
  2. # 2 楼答案

    首先,你的应用程序清单是否包含写入SD卡的必要权限

    <manifest ...>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        ...
    </manifest>
    

    其次,我不确定你是否有正确的SD卡路径The API docs suggest

    File sdPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String filename = Long.toString(System.currentTimeMillis())+".jpg";
    File pictureFile;
    pictureFile = new File(sdPath, filename);
    

    编辑:要使用URL显示图像,可以尝试:

    FileInputStream inputStream = new FileInputStream(pictureFile);  
    Drawable d = Drawable.createFromStream(inputStream, pictureFile.getName());
    imageViewer.setImageDrawable(d);
    
  3. # 3 楼答案

    1-确保你拥有必要的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    2.在手机上创建一个目录

     public static void createDirectory(Context context) {
    
          String directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/YOURDIRECTORYNAME";
                try {
                    File directory = new File(directoryPath);
                    if (!directory.exists()) {
                        directory.mkdirs();
                    }
                } catch (Exception e) {
                      e.printStackTrace();
                }
    
    
            }
    

    3-此方法用于拍照和创建文件

    private void takePictures() {
    
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
    
                File photoFile = null;
    
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
    
                }
    
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }
    
            }
    
        }
    

    4-创建文件并将其保存在文件夹中

         private File createImageFile() throws IOException {
                Calendar c = Calendar.getInstance();
                SimpleDateFormat timeData = new SimpleDateFormat("dd-MMM-yyyy-HH:mm:ss");
    //TODO declared global variables datePicture and imageFileName 
                datePicture = timeData.format(c.getTime());
                imageFileName = Config.IMAGE_NAME_DEFOULT + datePicture;
                File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/YOURDIRECTORYNAME");
                File image = File.createTempFile(imageFileName, ".png", storageDir);
               Log.v("test_photo" ,image.getName());
    //TODO declared global variables fileName and mCurrentPhotoPath
                fileName = image.getName();
                mCurrentPhotoPath = image.getAbsolutePath();
               return image;
            }
    

    5-将图片显示在imageView中,并保存在数据库中

          @Override
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    super.onActivityResult(requestCode, resultCode, data);
    
                    if (requestCode == REQUEST_IMAGE_CAPTURE) {
                        if (resultCode == getActivity().RESULT_OK) {
    
    
    //show directly 
    
    Bundle extras = data.getExtras();
    Bitmap bitmap = extras.get("data");
    youtImageView.setImageBitmap(bitmap);
    
                      /* here show in your imageView directly or
                 insert into database variables fileName and mCurrentPhotoPath 
        then you'll have to get it if you want to display from the DB */
    
                        } else if (resultCode == getActivity().RESULT_CANCELED) {
    
                        } 
                    }
    
    
                }
    

    我希望有帮助