Android:拍照并保存到指定文件夹

标签 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 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.
}

关于Android:拍照并保存到指定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767506/

相关文章:

android - 应用 -> 相机 -> 照片 -> 低分辨率

ios - 使用适用于 iOS 的 Adob​​e Builder (flex) 拍照

python - 如何使用 Python 更新 JSON 文件?

android - 为 Gradle 和 Cordova 5 指定签名配置

java - Android Studio 中的搜索栏

java - Android:与远程服务器通信的最佳方式是什么?

go - 将 exec.CommandContext 与 sg_inq sys 命令和超时一起使用永远不会返回

android - GCM服务器java实现空指针异常

android - 如何不将照片保存到本地?

java - 如何在Java中高效存储滚动播放项属性?