mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-17 01:32:21 +02:00
android native copy
This commit is contained in:
@ -1,18 +1,27 @@
|
||||
package eu.vcmi.vcmi.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.content.ContentResolver;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
|
||||
import org.libsdl.app.SDL;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
import java.lang.Exception;
|
||||
|
||||
import eu.vcmi.vcmi.Const;
|
||||
import eu.vcmi.vcmi.Storage;
|
||||
|
||||
/**
|
||||
@ -104,4 +113,50 @@ public class FileUtil
|
||||
target.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings(Const.JNI_METHOD_SUPPRESS)
|
||||
private static void copyFileFromUri(String sourceFileUri, String destinationFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
final Context ctx = SDL.getContext();
|
||||
final InputStream inputStream = new FileInputStream(ctx.getContentResolver().openFileDescriptor(Uri.parse(sourceFileUri), "r").getFileDescriptor());
|
||||
final OutputStream outputStream = new FileOutputStream(new File(destinationFile));
|
||||
|
||||
copyStream(inputStream, outputStream);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Log.e("copyFileFromUri failed: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings(Const.JNI_METHOD_SUPPRESS)
|
||||
private static String getFilenameFromUri(String sourceFileUri)
|
||||
{
|
||||
try
|
||||
{
|
||||
String fileName = "";
|
||||
|
||||
final Context ctx = SDL.getContext();
|
||||
ContentResolver contentResolver = ctx.getContentResolver();
|
||||
Cursor cursor = contentResolver.query(Uri.parse(sourceFileUri), null, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
fileName = cursor.getString(nameIndex);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return fileName;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.e("getFilenameFromUri failed: ", e);
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user