gpt4 book ai didi

java - Android使用getExternalFilesDir保存图片不一致

转载 作者:行者123 更新时间:2023-12-02 03:39:13 25 4
gpt4 key购买 nike

在我的应用程序中,我尝试拍摄一张照片,保存它,然后使用我从 Android Developers tutorial 获得的代码将其添加到 ImageView 中。

private static final int REQUEST_IMAGE_CAPTURE = 1;
private String lastImagePath;

//Create temporary image file using getExternalFilesDir()
private File createImageFile() throws IOException{
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timestamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
lastImagePath = image.getAbsolutePath();
return image;
}

//Start picture intent and save to file
private void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!= null){
try{
photoFile = createImageFile();
}catch (IOException e){
Log.e(TAG, "Error Creating image file");
e.printStackTrace();
}
if (photoFile !=null){
Log.e(TAG, photoFile.getAbsolutePath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){

//Ensure the path has been set
if(lastImagePath !=null){
Log.e(TAG, "LastImagePath: " + lastImagePath);

//DEBUG:see if it actually created the file
File file = new File(lastImagePath);
if(file.exists()){
Log.e(TAG, "File exists");
}

//Decode file
Bitmap image = BitmapFactory.decodeFile(lastImagePath);

//DEBUG: see if image is null
if(image == null){
Log.e(TAG, "Image is null");
}else{
Log.e(TAG, "Image is not null");
}

//convert full sized image to thumbnail and add it to imageView
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, 200, 200);
image1.setImageBitmap(thumbnail);
}else{
Log.e(TAG, "lastImagePath is null");
}
}
}

有时,此代码会按预期工作并获取图像,保存它,将其加载回程序中并将其添加到 ImageView 中。但其他时候,它根本不起作用,似乎无法拍摄图像。

这是成功捕获图像的 logcat 输出,其中图像出现在 ImageView 中

05-04 17:27:49.218 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:03.886 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:04.359 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:28:04.825 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is not null

这是图像捕获失败的 logcat 输出,其中 ImageView 中没有图像

05-04 17:29:00.070 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.581 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is null

这似乎是随机的,并且 80%​​ 的时间都会失败。我使用 getExternalFilesDir 因为我希望照片对应用程序是私有(private)的。我还在 list 文件中添加了 WRITE_EXTERNAL_STORAGE 权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我也知道我可以使用图像中的缩略图,但我想要完整尺寸的图像,因为稍后我会将其添加到类中。

有人对出了什么问题有任何想法吗?

最佳答案

当您的应用程序处于后台时,您的进程将被终止。这对于 Android 来说是正常的,尽管您可能没有想到这里会出现这种情况。您需要将 EXTRA_OUTPUT 保留在已保存的实例状态 Bundle 中。

另请注意 since file: Uri values are being banned ,您应该考虑改用 FileProvider

This sample app证明了这两件事。诚然,代码更复杂:

/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import java.io.File;
import java.util.List;

public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private static final String AUTHORITY=
BuildConfig.APPLICATION_ID+".provider";
private static final String PHOTOS="photos";
private File output=null;
private Uri outputUri=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (savedInstanceState==null) {
output=new File(new File(getFilesDir(), PHOTOS), FILENAME);

if (output.exists()) {
output.delete();
}
else {
output.getParentFile().mkdirs();
}
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}

outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

if (savedInstanceState==null) {
i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
else {
List<ResolveInfo> resInfoList=
getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}

startActivityForResult(i, CONTENT_REQUEST);
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putSerializable(EXTRA_FILENAME, output);
}

@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(outputUri, "image/jpeg");
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
finish();
}
}
}
}

关于您的问题,我将输出位置保存在 onSaveInstanceState()Bundle 中,并在 onCreate() 中再次使用它> 如果我有一个正在恢复的 Bundle

关于java - Android使用getExternalFilesDir保存图片不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com