android 把file资源转file,将文件:Uri转换为Android中的文件

最佳方案

创建一个简单的FileUtil java类并用于创建,复制和重命名该文件

我用uri.toString(),uri.getPath()但不适合我。我终于找到了这个解决方案import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.provider.OpenableColumns;import android.util.Log;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class FileUtil {

private static final int EOF = -1;

private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

private FileUtil() {

}

public static File from(Context context, Uri uri) throws IOException {

InputStream inputStream = context.getContentResolver().openInputStream(uri);

String fileName = getFileName(context, uri);

String[] splitName = splitFileName(fileName);

File tempFile = File.createTempFile(splitName[0], splitName[1]);

tempFile = rename(tempFile, fileName);

tempFile.deleteOnExit();

FileOutputStream out = null;

try {

out = new FileOutputStream(tempFile);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

if (inputStream != null) {

copy(inputStream, out);

inputStream.close();

}

if (out != null) {

out.close();

}

return tempFile;

}

private static String[] splitFileName(String fileName) {

String name = fileName;

String extension = "";

int i = fileName.lastIndexOf(".");

if (i != -1) {

name = fileName.substring(0, i);

extension = fileName.substring(i);

}

return new String[]{name, extension};

}

private static String getFileName(Context context, Uri uri) {

String result = null;

if (uri.getScheme().equals("content")) {

Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

try {

if (cursor != null && cursor.moveToFirst()) {

result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (cursor != null) {

cursor.close();

}

}

}

if (result == null) {

result = uri.getPath();

int cut = result.lastIndexOf(File.separator);

if (cut != -1) {

result = result.substring(cut + 1);

}

}

return result;

}

private static File rename(File file, String newName) {

File newFile = new File(file.getParent(), newName);

if (!newFile.equals(file)) {

if (newFile.exists() && newFile.delete()) {

Log.d("FileUtil", "Delete old " + newName + " file");

}

if (file.renameTo(newFile)) {

Log.d("FileUtil", "Rename file to " + newName);

}

}

return newFile;

}

private static long copy(InputStream input, OutputStream output) throws IOException {

long count = 0;

int n;

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

while (EOF != (n = input.read(buffer))) {

output.write(buffer, 0, n);

count += n;

}

return count;

}}

在代码中使用FileUtil类try {

File file = FileUtil.from(MainActivity.this,fileUri);

Log.d("file", "File...:::: uti - "+file .getPath()+" file -" + file + " : " + file .exists());

} catch (IOException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值