android studio获取图像视图文件路径/uri

nwwlzxa7  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(337)

我正在尝试获取imageview的文件路径/uri,以保存到aws的s3数据库中。我尝试使用imageview的位图/drawable,但无法获取路径。是否有其他方法获取imageview的文件路径/uri?以下是我到目前为止所写的内容,但它不起作用:

private void uploadInputStream() {

        Uri path = Uri.parse("com.example.project/" + R.id.ivProfilePicture);
        String imgPath = path.toString();

        InputStream exampleInputStream = null;
        try {
            exampleInputStream = getContentResolver().openInputStream(path);

            Amplify.Storage.uploadInputStream(
                    "ExampleKey",
                    exampleInputStream,
                    result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
                    storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

以下是我使用上述代码得到的错误:

W/System.err: java.io.FileNotFoundException: Resource does not exist: android.resource://com.example.project/2131362045
W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:1180)
        at com.example.project.VerifySignUpActivity.uploadInputStream(VerifySignUpActivity.java:126)
        at com.example.project.VerifySignUpActivity.access$000(VerifySignUpActivity.java:32)
        at com.example.project.VerifySignUpActivity$1$1$1.accept(VerifySignUpActivity.java:82)
        at com.example.project.VerifySignUpActivity$1$1$1.accept(VerifySignUpActivity.java:73)
        at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$5.lambda$onResult$0(AWSCognitoAuthPlugin.java:387)
        at com.amplifyframework.auth.cognito.-$$Lambda$AWSCognitoAuthPlugin$5$KyBT3ChKClMRGiVUgh9F0DBkIxU.call(Unknown Source:4)
        at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$23.onResult(AWSCognitoAuthPlugin.java:1149)
        at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$23.onResult(AWSCognitoAuthPlugin.java:1145)
        at com.amazonaws.mobile.client.internal.InternalCallback.call(InternalCallback.java:75)
        at com.amazonaws.mobile.client.internal.InternalCallback.onResult(InternalCallback.java:62)
W/System.err:     at com.amazonaws.mobile.client.AWSMobileClient$11$1.onSuccess(AWSMobileClient.java:1911)
        at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:1020)
        at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1904)
        at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101)
        at java.lang.Thread.run(Thread.java:919)
7bsow1i6

7bsow1i61#

嘿,我就是这么做的,试试看

private void uploadInputStream() {

    Uri path = Uri.parse("com.example.project/" + R.id.ivProfilePicture);

    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

    InputStream exampleInputStream = null;
    File file = null;

    try {
      exampleInputStream =
      getContentResolver().openInputStream(Uri.parse(bitmap));

      file = new File(String.valueOf(inputStream)); //upload the file

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

让我知道进展如何

相关问题