我正在制作一个应用程序,直到现在我已经这样做了。
public class TrafficAdd extends Activity{
Button take,upload;
EditText sub,details;
Bitmap bmp;
Intent i;
ImageView iv;
final static int cameraData=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.trafficadd);
InputStream is=getResources().openRawResource(R.drawable.green);
bmp=BitmapFactory.decodeStream(is);
take=(Button) findViewById(R.id.trafficcamera);
upload=(Button) findViewById(R.id.trafficupload);
sub=(EditText) findViewById(R.id.eTtrafficSub);
details=(EditText) findViewById(R.id.eTtrafficDetails);
iv=(ImageView) findViewById(R.id.trafficpic);
take.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle extras=data.getExtras();
bmp=(Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
我从相机拍摄照片,我可以将其设置为Bitmap bmp。现在我需要将这个bmp保存在SD卡中的某个位置并在以后检索它以进行查看。 请帮忙!!!
答案 0 :(得分:0)
将图像存储在SD卡上:
public boolean storeImage(Bitmap imageData, String filename) {
//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//choose another format if PNG doesn't suit you
imageData.compress(CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}
由于此操作会将数据保存在外部存储器中,因此需要AndroidManifest.xml
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
从SD卡中检索图片:
File f = new File(android.os.Environment.getExternalStorageDirectory()+ "/myAppDir/myImages/"+your image file name);
ImageView mImgView = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView.setImageBitmap(bmp);