【问题标题】:Camera Intent not returning to calling Activity相机意图不返回调用活动
【发布时间】:2014-07-01 06:46:03
【问题描述】:

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

任何想法都将不胜感激。

【问题讨论】:

    标签: android android-intent photo android-camera-intent


    【解决方案1】:

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

    如果是其他人的情况,请在开始意图之前执行此操作:

    file.getParentFile().mkdirs();
    

    【讨论】:

    • 在android控制台中没有IOException(例如NoSuchFileOrDirecrtoy)或日志跟踪告诉我们活动卡住的原因,这不是一个android框架错误吗?我们应该如何理解发生了什么(除了打开 stackoverflow ......)
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      原来解决方案是我的疏忽:我没有在 OnPause 方法中删除 imageview 的事件处理程序 - 一旦我这样做了,它就会完美运行。

      【讨论】:

        猜你喜欢
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-06
        • 2013-04-07
        相关资源
        最近更新 更多