我有一个问题,即我拍照片,我想将其保存在内部存储空间中。但我不知道怎么做。谁可以建议一些解决方案?谢谢。 有我的代码:
public class ThreeFragment extends Fragment {
private static final String TAG = ThreeFragment.class.getSimpleName();
private static int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 1;
View root;
GridView gridView;
String mCurrentPhotoPath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_three, container, false);
dolist();
return root;
}
public void dolist() {
gridView = (GridView) root.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
openGallery();
break;
case 1:
takePhoto();
break;
case 2:
Log.d(TAG, "333333333333333333333");
break;
default:
break;
}
}
});
}
private void takePhoto() {
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
private String saveToInternalSorage(Bitmap bitmap) {
ContextWrapper cw = new ContextWrapper(getActivity());
File dic = cw.getDir("TEST", Context.MODE_PRIVATE);
File mypath = new File(dic, ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return dic.getAbsolutePath();
}
public void openGallery() {
Intent i = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE) {
if (resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
}
else if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
saveToInternalSorage(bitmap);
// setPath(saveToInternalSorage(bitmap));
}
}
}
}
答案 0 :(得分:1)
*Try This :
File mImageFile is path where you want to store you camera image file.
Uri tempURI = Uri.fromFile(mImageFile);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
activity.startActivityForResult(i, CHOOSE_CAMERA_RESULT);
then in your onActivityResult
@Override
public void onActivityResultRAW(int requestCode, int resultCode, Intent data)
{
super.onActivityResultRAW(requestCode, resultCode, data);
switch(requestCode)
{
case CHOOSE_CAMERA_RESULT:
{
if (resultCode == RESULT_OK)
{
// here you image has been save to the path mImageFile.
Log.d("ImagePath", "Image saved to path : " + mImageFile.getAbsoultePath());
}
}
break;
}
}*
答案 1 :(得分:0)
试试这个:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
cameraIntent.putExtra("return-data", true);
mActPanelFragment.startActivityForResult(cameraIntent, ActivityConstantUtils.CAMERA_REQUEST);
//方法
public Uri setImageUri() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), ActivityConstantUtils.STORED_IMAGE_PATH+"/Images/WOMCamera");
if (!file.exists()) {
file.mkdirs();
}
ActivityConstantUtils.mCurrentPhotoPath = (file.getAbsolutePath() + "/"+"IMG_"+ System.currentTimeMillis() + ".jpg");
Uri imgUri = Uri.fromFile(new File(ActivityConstantUtils.mCurrentPhotoPath));
if (file.canWrite()) {
}
return imgUri;
}
之后写出结果代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActivityConstantUtils.GALLERY_INTENT_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String imagePath = getFilePath(data);
}
}
private String getFilePath(Intent data) {
String imagePath;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imagePath = cursor.getString(columnIndex);
cursor.close();
return imagePath;
}