繁体   English   中英

Camera Intent没有返回调用Activity

[英]Camera Intent not returning to calling Activity

我已经阅读了很多关于这个问题的问题。 有些类似于我所经历的; 我没有成功地尝试了答案。 该代码适用于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;
}

任何想法将不胜感激。

在我的情况下,它没有返回,因为输出文件在我尚未创建的文件夹中。

如果是其他人的情况,请在启动anintent之前执行此操作:

file.getParentFile().mkdirs();

所以我找到了解决问题的方法。 如果文件不存在,则Camera Activity将不会返回。 我猜它在4.4上不起作用的原因是对文件系统的更改。 我没有将图像保存到媒体库并将文件加载回我的apps目录。 然后删除媒体文件。 我不将它留在媒体目录中的原因是,当应用程序被删除时,图像将被删除。

这是新代码。 首先是意图调用

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();
            }
        }
    }
}

在我们的场景中,我们的应用程序可以通过“按钮”点击或“图像视图”点击启动相机应用程序。 当使用按钮路径时,相机应用程序正确地按预期返回到调用Activity,但是当点击imageview时结果不一致 - 有时它会返回,有时后退按钮必须被多次点击。

事实证明解决方案是我的疏忽:我没有在OnPause方法中删除imageview的事件处理程序 - 一旦我做了它然后它工作得很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM