Android图像未存储在存储中

时间:2015-09-03 04:47:25

标签: java android android-layout android-camera

我试图通过我的存储上的相机存储新捕获的图像,然后在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);

                     }
                });
            }
        });

我的代码是否正确用于编写和显示图像?我的存储空间中没有任何图像,图像视图也没有被图像填充。我做错了什么?

3 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

首先,您的应用程序的清单是否包含写入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);

编辑:要使用网址显示图片,您可以尝试:

FileInputStream inputStream = new FileInputStream(pictureFile);  
Drawable d = Drawable.createFromStream(inputStream, pictureFile.getName());
imageViewer.setImageDrawable(d);

答案 2 :(得分:0)

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) {

                    } 
                }


            }

我希望它有所帮助!