475

Androidでaからfile: android.net.Uriaに変換する最も簡単な方法は何ですか?File

次のことを試しましたが、うまくいきません:

 final File file = new File(Environment.getExternalStorageDirectory(), "read.me");
 Uri uri = Uri.fromFile(file);
 File auxFile = new File(uri.toString());
 assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath());
4

23 に答える 23

898

あなたが欲しいのは...

new File(uri.getPath());

...そしてそうではありません...

new File(uri.toString());

注: uri.toString()形式の String を返しますが、形式"file:///mnt/sdcard/myPicture.jpg"uri.getPath()String を返します: "/mnt/sdcard/myPicture.jpg"

于 2011-12-03T19:23:22.043 に答える
83

使用する

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

直接、ファイルをコピーします。以下も参照してください。

https://developer.android.com/guide/topics/providers/document-provider.html

于 2016-07-25T13:08:29.700 に答える
36

Android + コトリン

  1. Kotlin Android 拡張機能の依存関係を追加します。

    implementation 'androidx.core:core-ktx:{latestVersion}'

  2. uri からファイルを取得します。

    uri.toFile()

Android + Java

一番上に移動してください ;)

于 2018-05-25T06:23:29.407 に答える
27

編集:申し訳ありませんが、以前にもっとよくテストする必要がありました。これはうまくいくはずです:

new File(new URI(androidURI.toString()));

URI は java.net.URI です。

于 2010-06-04T14:51:20.983 に答える
15

これはどれも私にとってはうまくいきません。これが実用的なソリューションであることがわかりました。しかし、私のケースは images に固有のものです。

String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
于 2014-04-10T19:42:11.757 に答える
-1

特に画像のソリューションを探している人は、ここにあります。

private Bitmap getBitmapFromUri(Uri contentUri) {
        String path = null;
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            path = cursor.getString(columnIndex);
        }
        cursor.close();
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        return bitmap;
    }
于 2014-03-22T18:23:18.693 に答える
-1

File imageToUpload = new File(new URI(androidURI.toString())); これが外部ストレージで作成したファイルである場合に機能します。

たとえば、file:///storage/emulated/0/(ディレクトリとファイル名)

于 2015-02-28T00:31:38.180 に答える
-1

次のコードにより、アドビ アプリケーションの共有 pdf ファイルをストリームとして取得し、Android アプリケーション パスに保存することができます。

Android.Net.Uri fileuri =
    (Android.Net.Uri)Intent.GetParcelableExtra(Intent.ExtraStream);

    fileuri i am getting as {content://com.adobe.reader.fileprovider/root_external/
                                        data/data/com.adobe.reader/files/Downloads/sample.pdf}

    string filePath = fileuri.Path;

   filePath I am gettings as root_external/data/data/com.adobe.reader/files/Download/sample.pdf

      using (var stream = ContentResolver.OpenInputStream(fileuri))
      {
       byte[] fileByteArray = ToByteArray(stream); //only once you can read bytes from stream second time onwards it has zero bytes

       string fileDestinationPath ="<path of your destination> "
       convertByteArrayToPDF(fileByteArray, fileDestinationPath);//here pdf copied to your destination path
       }
     public static byte[] ToByteArray(Stream stream)
        {
            var bytes = new List<byte>();

            int b;
            while ((b = stream.ReadByte()) != -1)
                bytes.Add((byte)b);

            return bytes.ToArray();
        }

      public static string convertByteArrayToPDF(byte[] pdfByteArray, string filePath)
        {

            try
            {
                Java.IO.File data = new Java.IO.File(filePath);
                Java.IO.OutputStream outPut = new Java.IO.FileOutputStream(data);
                outPut.Write(pdfByteArray);
                return data.AbsolutePath;

            }
            catch (System.Exception ex)
            {
                return string.Empty;
            }
        }
于 2018-08-31T16:18:22.837 に答える