"我从ImageView获取图像并将其保存在根目录中,但是当我使用图库来拍摄相同的图像时,创建的文件夹在图库选取器中不可见。但是当我从文件管理器中检查文件夹时,它会向我显示已创建的带有图像"的文件夹, #I已使用此代码将图像保存在我的目录中。#
public void saveImageToSdCard(Bitmap bitmap1) {
File sdCard = Environment.getExternalStorageDirectory();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
if (sdCard != null) {
File directory = new File(sdCard.getAbsolutePath() + "/My_folder");
if (directory.exists()) {
directory.delete();
Log.e("it run","directoty is exists");
}
directory.mkdirs();
String strImageName = File.separator + "IMG_" + timeStamp + ".jpg";
File file = new File(directory, strImageName);
// strFinalImage = file.getAbsolutePath();
// Log.e(TAG + "strFinalImage22", strFinalImage + "");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
fileOutputStream.write(bytes.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
// Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
} else {
Toast.makeText(getActivity(),"Sd card mount", Toast.LENGTH_SHORT).show();
}
}
-------------------------------------------------------------------
# Intent code for gallery #
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 0);
-------------------------------------------------------------------
# OnActivityResultCode() #
Uri pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
Log.e("Gallery", "File URI:" + imagePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
cursor.close();