我无论怎么做都无法保存相机拍摄的图像。我该怎么做呢?

问题描述 投票:0回答:1

我已经按照一切的点从 这个 安卓官网。

我已经存储了 createTempFile()的路径,在 currentImagePath 在我的代码中。打印,显示路径。但是,似乎在该路径上没有数据。

我的逻辑是将该路径上的数据复制到一个永久路径上。

btnTakePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                captureImage(v);
                /*try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }*/

                //System.out.println(currentImagePath);
                File from = new File(currentImagePath);
                File storageFileDir = getFilesDir();
                byte[] bArray = new byte[(int) from.length()];
                try{
                    fi = new FileInputStream(from);
                    fi.read(bArray);
                    fi.close();

                }catch(IOException ioExp){
                    ioExp.printStackTrace();
                }
                File picStorageDir = new File(storageFileDir,"myPics");
                perStorageDir = picStorageDir;
                if(!picStorageDir.isDirectory())
                {
                    picStorageDir.mkdirs();
                }
                System.out.println(perStorageDir.getAbsolutePath());
                System.out.println("From length: "+from.length());
                try {
                    String[] t = currentImagePath.split("/"); // this is just to get image name
                    File temp = new File(picStorageDir,t[t.length-1]); // this is to set file with image name
                    ff = new FileOutputStream(temp);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    ff.write(bArray);
                } catch (IOException e) {
                    e.printStackTrace();
                }
});

下面是 captureImage() 办法

public void captureImage(View view)
    {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if(cameraIntent.resolveActivity(getPackageManager())!=null)
        {
            File imageFile = null;
            try {
                imageFile = getImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(imageFile!=null)
            {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, IMAGE_REQUEST);
            }
            System.out.println("imageFile length:"+imageFile.length());

        }

    }

这是日志输出

2020-04-30 14:19:32.646 31226-31226/com.example.ccalculator I/System.out: currImPath: /storage/emulated/0/Android/data/com.example.ccalculator/files/Pictures/jpg_20200430_1419032_793088582.jpg
2020-04-30 14:19:32.657 31226-31226/com.example.ccalculator I/System.out: imageFile length:0
2020-04-30 14:19:32.657 31226-31226/com.example.ccalculator I/System.out: /storage/emulated/0/Android/data/com.example.ccalculator/files/Pictures/jpg_20200430_1419032_793088582.jpg
2020-04-30 14:19:32.658 31226-31226/com.example.ccalculator I/System.out: permanent storage dir: /data/user/0/com.example.ccalculator/files/myPics
2020-04-30 14:19:32.658 31226-31226/com.example.ccalculator I/System.out: From length: 0

目前,我还没有从应用程序中捕获图像。所以我想,也许一旦我捕捉到图像,数据就会存储在各自的路径上。我试着在应用中添加 Thread.sleep(1000); 但即使这样也无济于事。

java android android-camera android-file
1个回答
0
投票

捕获是在回调时回来的,即 startActivityForResult 并没有暂停线程,所以添加一些类似于...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK) {

把你的代码放在回调中处理新图像。

© www.soinside.com 2019 - 2024. All rights reserved.