【问题标题】:Can i restrict to user, videos will save only in internal memory in Android programmatically我可以限制用户吗,视频将以编程方式仅保存在Android的内部存储器中
【发布时间】:2018-05-03 11:08:37
【问题描述】:

在我的应用程序中,有一项功能可以捕获视频,成功上传到服务器后,视频将从手机内存中删除。

我只想使用内存来保存视频。

我可以通过应用程序限制用户,他们只能使用内部存储器。

到目前为止我做了什么。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            videoUri = data.getData();
            try {
                filePath=getFilePath(getActivity(),videoUri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }


            file=new File(filePath);
            Log.e("before compression",file.getAbsolutePath()+"");

            new VideoCompressor().execute();

        }
    }

@SuppressLint("NewApi")
    public static String getFilePath(Context context, Uri uri) throws URISyntaxException {
        String selection = null;
        String[] selectionArgs = null;
        // Uri is different in versions after KITKAT (Android 4.4), we need to
        if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            } else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);
                uri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            } else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
                if ("image".equals(type)) {
                    uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                selection = "_id=?";
                selectionArgs = new String[]{
                        split[1]
                };
            }
        }
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {
                    MediaStore.Images.Media.DATA
            };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver()
                        .query(uri, projection, selection, selectionArgs, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

在这里我得到像 /storage/6638-3139/DCIM/Camera/20180502_125532.mp4(当手机中有sd卡时) /storage/emulated/0/20180502_125532.mp4(当手机中没有SD卡时)

现在我只想将此视频保存在内存中(是否存在 SD 卡)。

【问题讨论】:

标签: android


【解决方案1】:

在 android 中,如果您的应用的内部存储目录由您的应用的包名称指定,位于 Android 文件系统的特殊位置,可以使用以下 API 访问该位置。 将文件保存到内部存储时,您可以获得相应的

通过调用以下两种方法之一将目录作为文件:

getFilesDir() 返回一个代表应用程序内部目录的文件。 getCacheDir() 返回一个 File 代表应用程序临时缓存文件的内部目录。确保在不再需要每个文件时将其删除,并对您在任何给定时间使用的内存量实施合理的大小限制,例如 1MB。

您可以调用 openFileOutput() 来获取一个 FileOutputStream,它会写入您内部目录中的文件。例如,以下是如何将一些文本写入文件:

 String filename = "myfile";
String outputString = "Hello world!";

try {
    FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
    outputStream.write(outputString.getBytes());
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}

try {
    FileInputStream inputStream = openFileInput(filename);
    BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder total = new StringBuilder();
    String line;
    while ((line = r.readLine()) != null) {
        total.append(line);
    }
    r.close();
    inputStream.close();
    Log.d("File", "File contents: " + total);
} catch (Exception e) {
    e.printStackTrace();
}

更多信息请查看Save files on device storage

【讨论】:

  • 当我捕获 1 个视频时,我得到 uri 之后我需要 filepath 。如何将上述代码用于 uri。我如何获取文件路径?你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 2012-01-01
相关资源
最近更新 更多