但它没有回答我的问题。我有一个外部SD卡,我想从我的应用程序访问,并将图像从它复制到内部存储。这可能吗? 我正在使用android nouget
这是我到目前为止所做的......
public static List<StorageInfo> getStorageList() {
List<StorageInfo> list = new ArrayList<StorageInfo>();
String def_path = Environment.getExternalStorageDirectory().getPath();
boolean def_path_removable = Environment.isExternalStorageRemovable();
String def_path_state = Environment.getExternalStorageState();
boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
|| def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);
HashSet<String> paths = new HashSet<String>();
int cur_removable_number = 1;
if (def_path_available) {
paths.add(def_path);
list.add(0, new StorageInfo(def_path, def_path_readonly, def_path_removable, def_path_removable ? cur_removable_number++ : -1));
}
BufferedReader buf_reader = null;
try {
buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
String line;
Log.d(TAG, "/proc/mounts");
while ((line = buf_reader.readLine()) != null) {
Log.d(TAG, line);
if (line.contains("vfat") || line.contains("/mnt")) {
StringTokenizer tokens = new StringTokenizer(line, " ");
String unused = tokens.nextToken(); //device
String mount_point = tokens.nextToken(); //mount point
if (paths.contains(mount_point)) {
continue;
}
unused = tokens.nextToken(); //file system
List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
boolean readonly = flags.contains("ro");
if (line.contains("/dev/block/vold")) {
if (!line.contains("/mnt/secure")
&& !line.contains("/mnt/asec")
&& !line.contains("/mnt/obb")
&& !line.contains("/dev/mapper")
&& !line.contains("tmpfs")) {
paths.add(mount_point);
list.add(new StorageInfo(mount_point, readonly, true, cur_removable_number++));
}
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (buf_reader != null) {
try {
buf_reader.close();
} catch (IOException ex) {}
}
}
return list;
}
void open()
{
String sdCardPath = getStorageList().get(1).path+"/DCIM/100_FLIR/"; //placed it statically for now...
File imagesFolder = new File(Environment.getExternalStorageDirectory(), context.getString(R.string.FolderName));
if (!imagesFolder.exists())
{
imagesFolder.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
File image = new File(imagesFolder, "IM_" + timeStamp + ".jpeg");
imagepath = image.getAbsolutePath();
Uri uriImagePath = GlobalFunctions.getFileURI(context,image);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(path);
intent.setDataAndType(uri, "*/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
intent.putExtra("return-data", true);
startActivityForResult(intent,458);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 458 && resultCode == Activity.RESULT_OK)
{
File f = new File(imagepath);
try {
f.createNewFile();
copyFile(new File(getRealPathFromURI_API19(this,data.getData())), f); //getRealPathFromURI_API19 is returning empty for files on sd card
} catch (IOException e) {
globalVariables.AppLogging(GlobalVariables.ERROR_GENERAL_MSG_LOG,e.toString(), LogFileName.ErrorLogFilename);
}
}
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
// Image pick from recent
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = {MediaStore.Images.Media.DATA};
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{id}, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}