我正在尝试拍照并将其保存到手机存储器中的自定义位置。每次尝试将图像保存到应用程序 / data / data / [package] 的默认位置。我想存储在 / emulated / 0 / My_clicks / 之类的自定义位置。我未使用任何SD卡,所以我想将图像保存在手机内存中。当我在Google上搜索时,图像保存在SD卡的公共文件夹中,而不是自定义位置。
我的代码如下:
FirstFragment.java
public void onCreateView(.....)
{
// ...
mphotobutton=(ImageButton)v.findViewById(R.id.crime_imagebutton);
mphotobutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(getActivity(),SecondActivity.class);
startActivityForResult(i,REQUEST_PHOTO);
}
});
// ...
}
public void onActivityResult(int requestcode, int resultcode, Intent data)
{
if(requestcode==REQUEST_PHOTO)
{
String filename=data.getStringExtra(SecondFragment.EXTRA_PHOTO_FILENAME);
if(filename!=null)
{
Log.d(TAG, "filenam: " + filename);
Photo p =new Photo(filename);
Log.i(TAG,"Crime: "+mCrime.getTitle() +" has a photo");
BitmapDrawable b=null;
if(p!=null)
{
String path = getActivity().getFileStreamPath(p.getfilename()).getAbsolutePath();
b=PictureUtils.getScaledDrawable(getActivity(),path);
}
mphotoview.setImageDrawable(b);
}
}
}
SecondFragment.java
public void onCreateView(....)
{
// ...
takepicturebutton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
if(mcamera!=null)
mcamera.takePicture(mshuttercallback,null,mjpegcallback);
}
});
...
} // onCreateView()
private Camera.ShutterCallback mshuttercallback= new Camera.ShutterCallback()
{
public void onShutter()
{
// display the progress indicator
mprogresscontainer.setVisibility(View.VISIBLE);
}
};
private Camera.PictureCallback mjpegcallback=new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//create a filename
String filename= UUID.randomUUID().toString()+".jpg";
// save the jpeg data to disk
FileOutputStream os=null;
boolean success=true;
try
{
os=getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
os.write(data);
}
catch (Exception e)
{
Log.e(TAG,"Error writing to file "+filename,e);
success=false;
}
finally
{
try
{
if(os!=null)
os.close();
}
catch (Exception e)
{
Log.e(TAG,"Error closing file "+filename,e);
success=false;
}
} // finally
if(success)
{
Log.i(TAG, "JPEG saved at " + filename);
Intent i = new Intent();
i.putExtra(EXTRA_PHOTO_FILENAME,filename);
getActivity().setResult(Activity.RESULT_OK,i);
}
else
getActivity().setResult(Activity.RESULT_CANCELED);
getActivity().finish();
} // onPictureTaken()
}; // mjpegcallback
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
图片保存在名为 SecondFragment.java 的SecondActivity片段中。如何将图像保存到手机存储器中的自定义位置(例如: / storage / emulated / 0 / my_click / )不在SD卡中(我没有使用任何SD卡)?如何编码保存图像?
请帮助我。
答案 0 :(得分:0)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)将为您提供/ storage / emulated / 0 / Documents的路径。您还需要
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
请注意,对于Android,外部存储(并非必需)不是SD卡。
答案 1 :(得分:0)
最终我提出了解决方案。...感谢亚历克斯·科恩
查看上面的代码,然后与下面的代码进行比较
首先请确保使用Java代码在目录“ / storage / emulated / 0 / ”中创建文件夹“ My_clicks ”或手动创建自己的文件夹...我确实手动
private Camera.PictureCallback mjpegcallback=new Camera.PictureCallback()
{
......
}
public void onPictureTaken(byte[] data, Camera camera)
{
String filename="MrCrime-"+new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date())+".jpg";
// save the jpeg data to disk
FileOutputStream os=null;
boolean success=true;
// getExternalStorageDirectory() ==> this method return directory path "/storage/emulated/0/" THANKS TO Alex Cohn
String imagepath=Environment.getExternalStorageDirectory()+"/My_clicks/"+filename;
File imagefile=new File(imagepath);
try
{
os=new FileOutputStream(imagefile);
//os=getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
os.write(data);
}
catch(Exception e)
{
Log.e(TAG,"Error writing to file "+filename,e);
success=false;
}
.........
} // mjpegcallback
public void onActivityResult(int requestcode, int resultcode, Intent data)
{
if(requestcode==REQUEST_PHOTO)
{
String imagename=data.getStringExtra(SecondFragment.EXTRA_PHOTO_FILENAME);
if(imagename!=null)
{
Log.d(TAG, "imagename: " + imagename);
Photo p =new Photo(imagename);
Log.i(TAG,"Crime: "+mCrime.getTitle() +" has a photo");
//Photo p = mCrime.getphoto();
BitmapDrawable b=null;
if(p!=null)
{
//String path = getActivity().getFileStreamPath(p.getfilename()).getAbsolutePath();
//String filename = p.getfilename();
String imagepath = Environment.getExternalStorageDirectory()+"/My_clicks/"+imagename;
b=PictureUtils.getScaledDrawable(getActivity(),imagepath);
}
mphotoview.setImageDrawable(b);
}
}
}
希望有人会帮助您……