我通过startActivityForResult
通过android.provider.MediaStore.ACTION_IMAGE_CAPTURE
和android.provider.MediaStore.EXTRA_OUTPUT
来调用我的相机,希望在onActivityResult
但是当我拍下照片后点击复选标记的那一刻,它应该被送回的那一刻,我收到一条错误消息,松散地转换为"相机错误 - 保存其中一个时出现问题你的图像或剪辑" - 我可以关闭对话框或向Google Dev团队发送反馈。
此时的Logcat输出是:
09-01 22:01:46.157 24523-24523/com.google.android.GoogleCamera E/CAM_FatalErrorHandler﹕ Handling Media Storage Failure:
java.lang.Exception
at com.android.camera.FatalErrorHandlerImpl.onMediaStorageFailure(FatalErrorHandlerImpl.java:37)
at com.android.camera.PhotoModule.onError(PhotoModule.java:274)
at com.android.camera.PhotoModule.onCaptureDone(PhotoModule.java:1289)
at com.android.camera.PhotoModule$8.onClick(PhotoModule.java:593)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
09-01 22:01:46.158 24523-24523/com.google.android.GoogleCamera E/CAM_CameraUtil﹕ Show fatal error dialog
我的代码(清除了无关的部分):
private File tmpFile;
private Uri tmpFileURI;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// TempFile needs to be created for the camera to store the fullsize captured image
// tmpFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + MainActivity.APP_BASE_FOLDER + "/tmp.jpg");
try
{
tmpFile = File.createTempFile("my_app", ".jpg");
} catch (IOException e)
{
e.printStackTrace();
}
tmpFileURI = Uri.fromFile(tmpFile);
chooseCamera.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tmpFileURI);
startActivityForResult(takePicture, CreateModifiyEventFragment.CHOOSE_BY_CAMERA);//zero can be replaced with any action code
}
});
chooseLibrary.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, CreateModifiyEventFragment.CHOOSE_BY_LIBRARY);//one can be replaced with any action code
}
});
}
看到的常量声明如下:
public final static int CHOOSE_BY_CAMERA = 1;
public final static int CHOOSE_BY_LIBRARY = 0;
永远不会达到回调代码,所以我只是为您节省更多代码。 您可以看到我通过tmpFile和外部存储上的普通文件尝试了它。
权限也设置
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我不知道是什么触发了这个错误或者我错过了什么。我使用其他SO答案来创建此代码,但我没有看到任何差异。
非常感谢任何帮助。提前谢谢SO!
答案 0 :(得分:4)
File.createTempFile()
不太可能在Android上为您提供有用的功能。更重要的是,你无法知道第三方相机应用程序是否可以在那里写。在外部存储空间中选择一个位置(例如getExternalFilesDir()
或Environment.getExternalStoragePublicDirectory()
):
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
此外,您不需要CAMERA
权限,因为您没有拍照 - 您要求第三方应用拍照。