相机意图未返回到调用活动

8

我已经阅读了很多关于这个问题的提问。有些类似于我所经历的;我尝试了答案,但没有成功。该代码可以在HTC Android 4.2上运行,但无法在Nexus 7 Android 4.4上运行。我已经修改了存储目录,使其适用于Android 4.4,所以这不是问题的原因。

如果我使用相机意图,它永远不会返回

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

如果我使用

,那么它会返回什么。

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);

但是它不会保存文件。

以下是完整代码。 调用意图。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
    try
    {
        final File photoFile = createImageFile();

        if (photoFile != null)
        {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            //takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
            startActivityForResult(takePictureIntent, 1);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

文件名
private File createImageFile() throws IOException
{
    if (mStorageDirectory == null)
    {
        createInitialStorageDirectory();
        setupFolders();
        mScrollList.notifyDataSetInvalidated();
    }

    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "slide_" + timeStamp;
    File storageDir = new File(mStorageDirectory);
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);
//        image.setWritable(true, false);

    // Save a path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

回调函数
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == -1)
    {

    }
}

根目录加保存目录。
static String mBasePath = "/slides/";

private String getRootDirectory()
{
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state))
    {
        if (Build.VERSION.SDK_INT >= 19)
            return mView.getContext().getFilesDir() + mBasePath;
        else
            return Environment.getExternalStorageDirectory() + "/Android/data/com.hdms.manager" + mBasePath;
        //return Environment.getExternalStorageDirectory() + mBasePath;
    }

    return mBasePath;
}

欢迎提出您的想法。

3个回答

7
在我的情况下,它没有返回结果,因为输出文件在我尚未创建的文件夹中。 如果有其他人遇到这种情况,请在开始anintent之前执行以下操作:
file.getParentFile().mkdirs();

7
在安卓控制台中,如果出现IOException(如NoSuchFileOrDirectory)或日志跟踪没有通知我们活动被卡住的原因,这不是安卓框架的一个bug吗?除了访问stackoverflow之外,我们该如何理解发生了什么事? - GyRo

6
所以我已经找到了解决问题的方法。如果文件不存在,那么相机活动将不会返回。我猜测它在 4.4 上无法工作的原因是对文件系统的更改。我没有将图像保存到媒体库并将文件加载回我的应用程序目录。媒体文件随后被删除。我不把它留在媒体目录的原因是,当应用程序被删除时,图像也将被删除。

这是新代码。首先是 Intent 调用:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
    Uri mImageUri = App.mApplication.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    mCurrentPhotoPath = mImageUri.getPath();

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(takePictureIntent, 1);
}

然后是回调函数。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == -1)
    {
        if (data != null)
            mCurrentPhotoPath = String.valueOf(data.getData());

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = App.mApplication.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null);
        if (cursor == null)
            return;
        // find the file in the media area
        cursor.moveToLast();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        File source  = new File(filePath);
        cursor.close();
        // create the destination file
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "slide_" + timeStamp;
        File destination = new File(mStorageDirectory, imageFileName + ".jpg");

        // copy the data
        if (source.exists())
        {
            try
            {
                FileChannel src = new FileInputStream(source).getChannel();
                FileChannel dst = new FileOutputStream(destination).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();

                source.delete();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

0
在我们的场景中,我们的应用程序能够通过“按钮”单击或“imageview”单击启动相机应用程序。当使用按钮路由时,相机应用程序会按预期正确返回到调用Activity,但是当点击imageview时,结果不一致 - 有时它会返回,有时必须多次点击后退按钮。
事实证明,解决方案是我的疏忽:我没有在OnPause方法中删除imageview的事件处理程序 - 一旦我这样做了,它就完美地工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接