问题
I've followed the instructions on android's website on how to create a custom camera application and so far i made it. I've created a layout with a take a picture button and it works fine, the images are saved on the sdcard using the Camera.PictureCallback
.
The thing is that while I have my phone connected to the PC with a cable, if I browse to the directory where I save the images from the app, they are not showing, even if I refresh the directory. If I use them in the app for something, it works fine, which means that they are saved in the directory and the path is correct. I can also see them using a File Explorer
on the phone.
However, if I use the Windows Explorer
, they aren't showing at all until some time passes.
Is that normal or am I missing something here?
The phone (Samsung Galaxy S6) is connected to the PC as a Media Device (MTP) but I also tried connecting it as a Camera (PTP) and it is the same.
This is the sample code that I use to save the images:
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
String timeStamp = new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date());
String imageFileName = "MyTestPhoto_" + timeStamp;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File mediaStorageDir = new File(SAVE_IMAGE_PATH);
if (!mediaStorageDir.exists())
mediaStorageDir.mkdir();
File pictureFile = new File(mediaStorageDir + imageFileName + ".jpg");
if (pictureFile == null){
Log.d("PictureCallback", "Error creating media file, check storage permissions.");
return;
}
try{
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
}catch (FileNotFoundException e){
Log.d("PictureCallback ", "File not found: " + e.getMessage());
}catch (IOException ex){
Log.d("PictureCallback ", "Error accessing file: " + ex.getMessage());
}
photoButton.setClickable(true);
camera.startPreview();
}
};
From what I found on the internet, this might help:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse(mediaStorageDir + imageFileName + ".jpg")));
But it doesn't, and in fact the app crashes now:
Activity com.example.testApplicationCamera.CameraActivity has leaked IntentReceiver com.example.testApplicationCamera.CameraActivity$1@28968130 that was originally registered here. Are you missing a call to unregisterReceiver()?
It seems like this isn't supported on Android 4.4 KitKat and above. I don't think that I actually need it, because I am not trying to make it visible in a gallery or anything, it actually works fine, just out of curiosity, why doesn't it appear in the Windows Explorer?
回答1:
From what I found on the internet, this might help:
That was never a proper solution and fortunately has been blocked now.
Use MediaScannerConnection
and its static scanFile()
method to alert the MediaStore
to index your files.
why doesn't it appear in the Windows Explorer?
Windows is connecting to the Android device via the Media Transfer Protocol (MTP). What Android serves up over the MTP connection is not what is in the filesystem, but rather what is indexed by MediaStore
.
来源:https://stackoverflow.com/questions/32972486/custom-camera-photos-are-not-showing-in-windows-explorer