Reputation: 721
I have this code:
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();
}
}
}
i know it works (the photo is taken and because of dropbox, i'm sure it's working), but it doesn't appear in the ImageView and in the file manager! how can i put the photo in a specified directory (/sdcard/my-app/) and in the imageview
Upvotes: 2
Views: 5579
Reputation: 572
Set your file path:
public final static String APP_PATH_SD_CARD = "/bao";
You can save the image as following:
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();
}
You can now retrieve that image from that path:
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.
}
Upvotes: 1