我在活动中加载了片段。
在这个片段中,我将一个事件onClick设置为一个按钮来拍照。我想将此图片保存在特定文件夹的内部存储空间中。
这是我的片段类,我已经尝试了很多不同的解决方案,但我不明白为什么mCurrentPhotoPath被创建但未被所拍照片填充(SkImageDecoder :: Factory返回null)。
这是我的片段类(我删除了部分代码)
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;
}
}
提前感谢您的帮助..
答案 0 :(得分:3)
让我在Android中提及关键概念,了解您的代码无法正常工作的原因。 ACTION_IMAGE_CAPTURE
意图启动另一个应用。在这种情况下,一个相机应用程序来完成一项工作。 EXTRA_OUTPUT
告诉应用程序调用(即相机应用程序)将拍摄的照片保存在文件系统中。
问题是,应用可能仅访问其自己的私有内部存储空间。因此,相机应用程序无法将图片保存在来电者的应用程序(即您的应用程序)私人存储空间内。
请注意,您呼叫的ContextWrapper.getDir
指向应用私有存储中的目录。
如果您想将图片存储在应用的私人存储空间中,您必须暂时将图片存储在两个应用都可以访问的位置,onActivityResult
将图片复制到您的应用中&# 39;私人存储。