【发布时间】:2017-09-29 06:46:44
【问题描述】:
我对 android 开发还很陌生,但我边走边学,我正在上课以了解更多信息。我有一个应用程序可以做一些事情,但我坚持的部分是处理相机和拍照。这个想法是应用程序将使用相机拍照并将图像设置为 ImageView 的“源”。如果我对 ImageView 使用位图,我可以做到这一点。但是,用户还应该能够在用相机拍摄并保存照片后将照片从他们的画廊导入应用程序。这就是我卡住的地方。我一直在关注本教程,了解如何设置它并让它工作,但它给了我一个奇怪的错误。 https://developer.android.com/training/camera/photobasics.html
我能够开始并通过“获取缩略图”部分,但之后我就卡住了。我不确定为什么它不会将我的照片保存到任何位置。
有代码说:
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
我意识到我需要在我的 res 文件夹中创建一个名为 xml 的文件夹,然后将一个名为 file_paths 的文件放入其中。这就是我放的地方
<Paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.android.postvu/files/pictures" />
</Paths>
非常感谢任何帮助
谢谢
我的(相关)Java 代码:
String mCurrentPhotoPath;
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
static final int REQUET_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
File photoFile = null;
try
{
photoFile = createImageFile();
}
catch (IOException ex)
{ }
if (photoFile != null)
{
Uri photoURI = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUET_TAKE_PHOTO);
}
}
}
private void galleryAddPic()
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
我的 Manifest.XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.postvu">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<uses-feature android:name="android.hardware.camera2"> </uses-feature>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
【问题讨论】:
-
那么您在标题中提到的错误在哪里?发布与错误相关的 logcat 堆栈跟踪。另外,在您的代码中,我可以看到您正在静音一个异常
IOException:catch (IOException ex) {}。这很糟糕,因为如果由于某种原因引发此异常,您将永远不会被告知!在catch块内放置一个Log.e("TAG", "error", ex)。很可能错误就在那里。 -
对不起。我忘了改标题
-
此外,您为什么不从您发布的 url 下载示例应用程序,看看您在做什么不同并更正它。 developer.android.com/shareables/training/…
标签: java android xml android-camera android-gallery