android - Camera Intent 未返回调用 Activity

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

我已经阅读了很多关于这个问题的问题。有些与我正在经历的相似;我尝试了没有成功的答案。该代码适用于 HTC Android 4.2,不适用于 Nexus 7 Android 4.4。我已经修改了存储目录以在 android 4.4 上工作,所以这不是问题。

如果我使用,相机 Intent 永远不会返回

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

如果我使用的话确实会返回

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);

但它不保存文件。

这是完整的代码。 调用 Intent

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

如有任何想法,我们将不胜感激。

最佳答案

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

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

file.getParentFile().mkdirs();

关于android - Camera Intent 未返回调用 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503968/

相关文章:

android - 始终接收 API 级别 +26 的广播(即使在后台)的广播接收器

android - SendBroadcast Intent 具有空额外内容

c++ - 将胶片负 RGB 转换为正 RGB 的算法

linux - 如何在 Linux 上从网络摄像头捕获静止图像

java - 延迟分组网络请求

java - 连接表 sqlite android

android - 如何在 NavigationView 中添加页脚按钮

android - 需要开发可以支持使用 Adob​​e Captivate 开发的模块的 Android 和 iOS 应用程序

按导航栏后退按钮时,Android Activity 返回到启动它的 Activity 而不是父 Activity

android - 我无法使用“共享”对话框共享图像