对不起标题,我不知道如何说出来,我的应用程序的问题是拍照并使用输入/输出流将其Uri内容写入新文件。到目前为止我有这个
Intent take_photo_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (take_photo_intent.resolveActivity(getActivity().getPackageManager()) != null)
{
try
{
// create file from a template
image_file = createFileForImage();
}
catch (IOException e)
{
e.printStackTrace();
}
// check if file is null
if(image_file != null)
{
// create uri using file and applying a provider
image_uri = FileProvider.getUriForFile(getContext(), PROVIDER, image_file);
take_photo_intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
image_path = image_uri.getPath();
// start activity
startActivityForResult(take_photo_intent, TAKE_PHOTO);
}
}
在这种情况下,返回位图是我需要在我的应用程序中显示缩略图质量图像
我的问题是尝试获取完整的缩放图像并将其保存到Android设备存储中的自定义文件夹。
我一直在关注本教程
https://developer.android.com/training/camera/photobasics.html#Save%20the%20Full-size%20Photo
我不是100%确定我正确设置了提供程序,应用程序编译和符文,我的问题是与Uri。
如果你回到我的应用程序的这个块
image_uri = FileProvider.getUriForFile(getContext(), PROVIDER, image_file);
take_photo_intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
image_path = image_uri.getPath();
image_file是我在外部存储中的文件夹中创建的File对象,该部分很好,因为我将该路径用于其他事情并且可以在文件管理器中验证它。直到稍后才会使用。
自应用程序运行以来,提供程序“似乎正常工作”
我的问题是image_uri。我的想法是,我在我创建的目录中保存了一个空文件,现在我需要将image_uri的内容读入该文件,所以我在onActivityResult中做了这个
// this will hold the Uri data
InputStream in_file = null;
// this will be used to write the input stream to the file
OutputStream out_file = null;
// check if Uri is null
if(image_uri != null) {
try {
// get the Uri data into an input stream
in_file = getContext().getContentResolver().openInputStream(image_uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// check for nulls
if (in_file != null && image_file != null) {
// create output stream linked to new file location
try {
out_file = new FileOutputStream(image_file);
Log.i("IMAGE", "open outsream");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// create temp byte array
byte[] image_bytes = null;
try {
// use apache tools to write bytes to that file using the outputstream
image_bytes = IOUtils.toByteArray(in_file);
} catch (IOException e) {
e.printStackTrace();
}
if (image_bytes != null && out_file != null) {
try {
out_file.write(image_bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
现在我想,好吧,我已经拍摄了提供商设置的图片,我在sd卡上创建了一个我认识有效的外部文件,并且我有一个输入流到uri和一个输出流到文件,并使用IOUtils.toByteArray来编写。
然而,我的问题是uri数据全部为空白。我的意思是,在IOUtils.toByteArray调用为0之后,字节全部为0或数组大小。所以我猜它一定是提供者的问题。我仍然不确定它是如何工作的,离开教程,我有这个
的AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.main.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
RES / XML / file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="my_images" path="Android/data/com.myapp/files/Pictures" />
正如我所说,我不确定这是如何工作的,默认情况下/ files / Pictures是我的应用程序内部存储中的文件夹?我必须做到吗?
当退出一些东西时,这就是我得到的
image_uri = FileProvider.getUriForFile(getContext(), PROVIDER, image_file);
Log.i("IMAGE", "DEBUG--->: " + image_uri.getPath());
I/IMAGE: DEBUG--->: /my_images/camera_shots/20160808_190857-1642881770.jpg
并且我从未创建过该文件夹,因此我对这一切是如何工作感到困惑。