gpt4 book ai didi

android - 保存图片 EXTRA_OUPUT 为空 onActivityResult

转载 作者:行者123 更新时间:2023-11-29 17:44:50 24 4
gpt4 key购买 nike

我在一个 Activity 中加载了 fragment 。

在此 fragment 中,我将事件 onClick 设置为按钮以拍照。我想将这张照片保存在内部存储中的特定文件夹中。

这是我的 fragment 类,我尝试了很多不同的解决方案,但我不明白为什么 mCurrentPhotoPath 被创建但没有被拍摄的照片填充(SkImageDecoder::Factory 返回 null)。

这是我的 fragment 类(我删除了部分代码)

public class Q3Fragment extends Fragment {

private OnFragmentInteractionListener mListener;
private Spinner field;
private int TAB_ID = 1;
private int Q_ID = 3;

private final static String DEBUG_TAG = "PHOTOMANAGER";
private String mCurrentPhotoPath;

private ImageView imagePreview;
private String filePath = "";
Intent takePictureIntent;
private PhotoManager pm;

public static Q3Fragment newInstance(String type) {
Q3Fragment fragment = new Q3Fragment();
Bundle args = new Bundle();
args.putString("type", type);
fragment.setArguments(args);
return fragment;
}
public Q3Fragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final View view = inflater.inflate(R.layout.fragment_q3, container, false);


/////////////

imagePreview = (ImageView) view.findViewById(R.id.q4imgPreview);



takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Button buttonPhoto = (Button) view.findViewById(R.id.q4BtnPhoto);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {

}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 11);
}


}

});

/////////////

return view;

}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Boolean valid,int qId);
}






@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
Log.d("Fragment PHOTOMANAGER","onActivityResult");
// lors du resultat de l'intent (lorsqu'on clique sur save la photo)

try {
handleSmallCameraPhoto();
} catch (IOException e) {
e.printStackTrace();
}

if(data != null) {
getActivity().getContentResolver().delete(data.getData(), null, null);
}


}


// on enregistre la photo
private void handleSmallCameraPhoto() throws IOException {


File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);

if(f.exists()){
Log.d(DEBUG_TAG,"FILE EXISTS " + mCurrentPhotoPath);
}

// here the bitmap not containing picture.
Bitmap mBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentUri);

imagePreview.setImageBitmap(mBitmap);
filePath = saveToInternalStorage(mBitmap);


}


// methode pour sauvegarder une photo (min ou full)
private String saveToInternalStorage(Bitmap bitmapImage){

ContextWrapper cw = new ContextWrapper(getActivity().getApplicationContext());

Log.d(DEBUG_TAG,"saveToInternalStorage");

File directory = cw.getDir("imageDir_"+getArguments().getString("type"), Context.MODE_PRIVATE);

String fileName;



fileName = "q" + Q_ID + ".jpg";


// Create imageDir
File mypath = new File(directory, fileName);

FileOutputStream fos = null;
try {

fos = new FileOutputStream(mypath);

// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(DEBUG_TAG,"FILE SAVED "+mypath.getAbsolutePath());
return mypath.getAbsolutePath();
}

private File createImageFile() throws IOException {
// Create an image file name
String imageFileName = "q" + Q_ID +"photo";
ContextWrapper cw = new ContextWrapper(getActivity().getApplicationContext());

Log.d(DEBUG_TAG,"saveToInternalStorage");

File directory = cw.getDir("imageDir_"+getArguments().getString("type"), Context.MODE_PRIVATE);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
directory /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
Log.d(DEBUG_TAG," createcontainer file "+mCurrentPhotoPath);
return image;
}


}

在此先感谢您的帮助..

最佳答案

让我提一下 Android 中的一个关键概念,需要用它来理解为什么您的代码不能按原样运行。 ACTION_IMAGE_CAPTURE Intent 启动另一个应用。在这种情况下,相机应用程序可以完成工作。 EXTRA_OUTPUT 告诉调用的应用程序(即相机应用程序)将拍摄的照片保存在文件系统中的何处。

问题是应用可能只能访问它们自己的私有(private)内部存储空间。因此,相机应用程序无法将图片保存在调用者应用程序(即您的应用程序)的私有(private)存储空间中。

请注意,您正在调用 ContextWrapper.getDir,它指向您应用的私有(private) 存储中的一个目录。

如果您想将图片存储在您应用的私有(private)存储中,您必须暂时将图像存储在两个应用都可以访问的位置,然后 onActivityResult 将图片复制到您应用的私有(private)存储中。

关于android - 保存图片 EXTRA_OUPUT 为空 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27391031/

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