1

ユーザーがビデオをインポートして編集できるようにするアプリケーションに取り組んでいます。このアプリケーションを使用すると、ユーザーはローカル ビデオ、または Google フォトやドライブからビデオをインポートできます。次のコードは機能していました

Intent i = new Intent();
i.setType("video/mp4");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, REQUEST_CODE);

次に、onActivityResult メソッドで、写真またはドライブからファイルにコピーします

if (resultCode == RESULT_OK) {
    Uri uri = data.getData();

    try {
       File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
       File app_directory = new File(storage, APP_NAME);
       if (!app_directory.exists())
           app_directory.mkdirs();

       String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
       String filename = String.format("VID_%s.mp4", timestamp);

       File file = new File(app_directory, filename);

       InputStream is = getContentResolver().openInputStream(uri);

       OutputStream output = new FileOutputStream(file);

       byte[] buffer = new byte[4096];
       int read;

       while ((read = is.read(buffer)) != -1)
            output.write(buffer, 0, read);

       output.flush();
       output.close();
    } catch (FileNotFoundException e) {
       Toast.makeText(this, "File Not Found", Toast.LENGTH_LONG).show();
       Log.e(TAG, "File Not Found", e);
    } catch (IOException e) {
       Toast.makeText(this, "Could not save the file", Toast.LENGTH_LONG).show();
       Log.e(TAG, "IOException", e);
    }
}

ただし、Google フォト アプリの最新の更新以降、どのアプリケーションでも開くことができない非常に小さなファイルしか取得できず、最も古いビデオの場合、FileNotFoundException が発生することがあります。

このようにファイルをリクエストする意図を変更するなど、いくつかの解決策を試しました

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/mp4");
startActivityForResult(i, REQUEST_CODE);

または FileDescriptor を介して InputStream を取得する

ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

InputStream is = new FileInputStream(fileDescriptor);

同じ結果で

4

0 に答える 0