将相机图像保存在手机记忆库中的自定义位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将相机图像保存在手机记忆库中的自定义位置相关的知识,希望对你有一定的参考价值。
我正在尝试拍照并将其保存到手机记忆库中的自定义位置。每次我尝试将图像保存到application / data / data / [package]的默认位置。我想存储在自定义位置,如/ emulated / 0 / My_clicks /。我没有使用任何SD卡,所以我想将图像保存在手机内存中。当我在Google上搜索时,我将图像保存在SD卡上的公共文件夹中,而不是保存在自定义位置。
我的代码看起来像这样:
first fragment.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);
}
}
}
second fragment.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
AnroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
图片保存在名为SecondFragment.java的SecondActivity片段中。如何将图像保存到手机存储器中的自定义位置(例如:/ storage / emulated / 0 / my_click /)NOT IN SD CARD(我没有使用任何SD卡)?如何编码保存图像?
请帮我。
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)将为您提供/ storage / emulated / 0 / Documents的路径。你还需要
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
请注意,对于Android,外部存储不是(必然)SD卡。
最后我做出了解决方案....感谢Alex Cohn
查看上面的代码并与下面的代码进行比较
首先确保使用java代码在目录“/ storage / emulated / 0 /”中创建文件夹“My_clicks”或手动制作自己的...我手动完成
SecondFragment.java
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
FirstFragment.java
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);
}
}
}
希望有人会帮助......
以上是关于将相机图像保存在手机记忆库中的自定义位置的主要内容,如果未能解决你的问题,请参考以下文章