private void createFile(byte[] fileData) throws IOException {
FileOutputStream out=null;
try {
//Create the directory
String mDirectoryPath = Environment.DIRECTORY_PICTURES + LinksAndKeys.DIRECTORY_PATH_FOR_IMAGES;
String mImageName = System.currentTimeMillis()+".jpg";
File root = Environment.getExternalStoragePublicDirectory(mDirectoryPath);
File dir = new File(root + File.separator);
if (!dir.exists()) dir.mkdir();
//Create file..
File file = new File(root + File.separator + mImageName);
file.createNewFile();
out = new FileOutputStream(file);
if(out!=null){
out.write(fileData);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
out.close();
}
}
private ArrayList<String> loadPhotosFromNativeGallery() {
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor imagecursorExternalUri = getActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");
Cursor imagecursorInternalUri = getActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");
Cursor[] cursorArray = { imagecursorExternalUri, imagecursorInternalUri};
MergeCursor mMergeCursor = new MergeCursor(cursorArray);
ArrayList<String> imageUrls = new ArrayList<String>();
for (int i = 0; i < mMergeCursor.getCount(); i++) {
mMergeCursor.moveToPosition(i);
int dataColumnIndex = mMergeCursor.getColumnIndex(MediaStore.Images.Media.DATA);
imageUrls.add(mMergeCursor.getString(dataColumnIndex));
System.out.println("=====> Array path => "+imageUrls.get(i));
}
return imageUrls;
}
答案 0 :(得分:0)
系统会在安装SD卡时扫描SD卡以查找任何新图像(和其他)文件。如果以编程方式添加文件,则必须扫描新文件。你可以使用以下方法来做到这一点:
MediaScannerConnection.scanFile(this, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);
您可以从this link
找到更多信息您可以直接插入数据:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "YourTitle");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);