Android 安卓:拍摄照片并保存到指定文件夹

Android 安卓:拍摄照片并保存到指定文件夹,android,camera,storage,directory,Android,Camera,Storage,Directory,我有以下代码: public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.inventario); Button scatta=(Button) findViewById(R.id.scatta_foto); scatta.setOnClickListener(new OnClickListener(){

我有以下代码:

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inventario);
    Button scatta=(Button) findViewById(R.id.scatta_foto);
    scatta.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, 2);
        }
    });
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 2) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ImageView test = (ImageView) findViewById(R.id.anteprima);
        test.setImageBitmap(photo);
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String currentDateandTime = sdf.format(new Date()).replace(" ","");
            FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bao/bao"+currentDateandTime+".jpg");
            photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
我知道它可以工作(照片是拍摄的,因为dropbox,我确信它可以工作),但它不会出现在ImageView和文件管理器中! 如何将照片放在指定目录(/sdcard/my app/)和imageview中设置文件路径:

public final static String APP_PATH_SD_CARD = "/bao";
您可以按以下方式保存图像:

String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD;

        try {
            File dir = new File(fullPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String currentDateandTime = sdf.format(new Date()).replace(" ","");
            OutputStream fOut = null;
            File file = new File(fullPath, currentDateandTime);
            file.createNewFile();
            fOut = new FileOutputStream(file);

            // 100 means no compression, the lower you go, the stronger the compression
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
现在,您可以从该路径检索该图像:

ImageView picture = (ImageView) findViewById(R.id.imageView);
String pathName = Environment.getExternalStorageDirectory().getPath() + "/boa/" + "nameofimage.png";
File path = new File(pathName);
if(path.exists()){
    BitmapFactory.Options options = new BitmapFactory.Options();
    bm = BitmapFactory.decodeFile(pathName, options);
    picture.setImageBitmap(bm);
}
 else{
     //Set default picture or do nothing.
}

它没有进入onActivityresult!可能是因为我有选项卡,我用replacecontentview更改了视图(但在这起作用之前)?不要忘记回收位图,调用bm.recycle();