我正在尝试使用Firebase的示例存储应用进行测试。不幸的是,它不适合我。我尝试了无数无济于事的事情。
我的错误也列在下面。我知道它告诉我没有这样的文件或目录,但我似乎无法弄清楚原因。我也确认我有外部存储的读/写权限
https://firebase.google.com/docs/storage/
https://github.com/firebase/quickstart-android/tree/master/storage
MainActivity.java
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.quickstart.firebasestorage;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener, EasyPermissions.PermissionCallbacks {
private static final String TAG = "Storage#MainActivity";
private static final int RC_TAKE_PICTURE = 101;
private static final int RC_STORAGE_PERMS = 102;
private static final String KEY_FILE_URI = "key_file_uri";
private static final String KEY_DOWNLOAD_URL = "key_download_url";
private BroadcastReceiver mDownloadReceiver;
private ProgressDialog mProgressDialog;
private FirebaseAuth mAuth;
private Uri mDownloadUrl = null;
private Uri mFileUri = null;
// [START declare_ref]
private StorageReference mStorageRef;
// [END declare_ref]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Initialize Firebase Storage Ref
// [START get_storage_ref]
mStorageRef = FirebaseStorage.getInstance().getReference();
// [END get_storage_ref]
// Click listeners
findViewById(R.id.button_camera).setOnClickListener(this);
findViewById(R.id.button_sign_in).setOnClickListener(this);
findViewById(R.id.button_download).setOnClickListener(this);
// Restore instance state
if (savedInstanceState != null) {
mFileUri = savedInstanceState.getParcelable(KEY_FILE_URI);
mDownloadUrl = savedInstanceState.getParcelable(KEY_DOWNLOAD_URL);
}
// Download receiver
mDownloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "downloadReceiver:onReceive:" + intent);
hideProgressDialog();
if (MyDownloadService.ACTION_COMPLETED.equals(intent.getAction())) {
String path = intent.getStringExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH);
long numBytes = intent.getLongExtra(MyDownloadService.EXTRA_BYTES_DOWNLOADED, 0);
// Alert success
showMessageDialog("Success", String.format(Locale.getDefault(),
"%d bytes downloaded from %s", numBytes, path));
}
if (MyDownloadService.ACTION_ERROR.equals(intent.getAction())) {
String path = intent.getStringExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH);
// Alert failure
showMessageDialog("Error", String.format(Locale.getDefault(),
"Failed to download from %s", path));
}
}
};
}
@Override
public void onStart() {
super.onStart();
updateUI(mAuth.getCurrentUser());
// Register download receiver
LocalBroadcastManager.getInstance(this)
.registerReceiver(mDownloadReceiver, MyDownloadService.getIntentFilter());
}
@Override
public void onStop() {
super.onStop();
// Unregister download receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mDownloadReceiver);
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putParcelable(KEY_FILE_URI, mFileUri);
out.putParcelable(KEY_DOWNLOAD_URL, mDownloadUrl);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
if (requestCode == RC_TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
if (mFileUri != null) {
uploadFromUri(mFileUri);
} else {
Log.w(TAG, "File URI is null");
}
} else {
Toast.makeText(this, "Taking picture failed.", Toast.LENGTH_SHORT).show();
}
}
}
// [START upload_from_uri]
private void uploadFromUri(Uri fileUri) {
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
// [START get_child_ref]
// Get a reference to store file at photos/<FILENAME>.jpg
final StorageReference photoRef = mStorageRef.child("photos")
.child(fileUri.getLastPathSegment());
// [END get_child_ref]
// Upload file to Firebase Storage
// [START_EXCLUDE]
showProgressDialog();
// [END_EXCLUDE]
Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
photoRef.putFile(fileUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Upload succeeded
Log.d(TAG, "uploadFromUri:onSuccess");
// Get the public download URL
mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
// [START_EXCLUDE]
hideProgressDialog();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Upload failed
Log.w(TAG, "uploadFromUri:onFailure", exception);
mDownloadUrl = null;
// [START_EXCLUDE]
hideProgressDialog();
Toast.makeText(MainActivity.this, "Error: upload failed",
Toast.LENGTH_SHORT).show();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
});
}
// [END upload_from_uri]
@AfterPermissionGranted(RC_STORAGE_PERMS)
private void launchCamera() {
Log.d(TAG, "launchCamera");
// Check that we have permission to read images from external storage.
String perm = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (!EasyPermissions.hasPermissions(this, perm)) {
EasyPermissions.requestPermissions(this, getString(R.string.rationale_storage),
RC_STORAGE_PERMS, perm);
return;
}
// Choose file storage location, must be listed in res/xml/file_paths.xml
File externalDir = Environment.getExternalStorageDirectory();
File file = new File(externalDir, "photos/" + UUID.randomUUID().toString() + ".jpg");
// Create content:// URI for file, required since Android N
// See: https://developer.android.com/reference/android/support/v4/content/FileProvider.html
mFileUri = FileProvider.getUriForFile(this,
"com.google.firebase.quickstart.firebasestorage.fileprovider", file);
// Create and launch the intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
startActivityForResult(takePictureIntent, RC_TAKE_PICTURE);
}
private void signInAnonymously() {
// Sign in anonymously. Authentication is required to read or write from Firebase Storage.
showProgressDialog();
mAuth.signInAnonymously()
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Log.d(TAG, "signInAnonymously:SUCCESS");
hideProgressDialog();
updateUI(authResult.getUser());
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e(TAG, "signInAnonymously:FAILURE", exception);
hideProgressDialog();
updateUI(null);
}
});
}
private void beginDownload() {
// Get path
String path = "photos/" + mFileUri.getLastPathSegment();
// Kick off download service
Intent intent = new Intent(this, MyDownloadService.class);
intent.setAction(MyDownloadService.ACTION_DOWNLOAD);
intent.putExtra(MyDownloadService.EXTRA_DOWNLOAD_PATH, path);
startService(intent);
// Show loading spinner
showProgressDialog();
}
private void updateUI(FirebaseUser user) {
// Signed in or Signed out
if (user != null) {
findViewById(R.id.layout_signin).setVisibility(View.GONE);
findViewById(R.id.layout_storage).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.layout_signin).setVisibility(View.VISIBLE);
findViewById(R.id.layout_storage).setVisibility(View.GONE);
}
// Download URL and Download button
if (mDownloadUrl != null) {
((TextView) findViewById(R.id.picture_download_uri))
.setText(mDownloadUrl.toString());
findViewById(R.id.layout_download).setVisibility(View.VISIBLE);
} else {
((TextView) findViewById(R.id.picture_download_uri))
.setText(null);
findViewById(R.id.layout_download).setVisibility(View.GONE);
}
}
private void showMessageDialog(String title, String message) {
AlertDialog ad = new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.create();
ad.show();
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_camera:
isExternalStorageWritable();
launchCamera();
break;
case R.id.button_sign_in:
signInAnonymously();
break;
case R.id.button_download:
beginDownload();
break;
}
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Toast.makeText(MainActivity.this, "YOU CAN READ AND WRITE",
Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(MainActivity.this, "YOU CANNOT!!!",
Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {}
}
我得到的错误如下:
07-23 03:15:57.006 23429-23429/com.google.firebase.quickstart.firebasestorage D/Storage#MainActivity: uploadFromUri:src:content://com.google.firebase.quickstart.firebasestorage.fileprovider/external/4b68b0e9-c950-4cf1-bee3-2bedfcda52dd.jpg
07-23 03:15:57.086 23429-23429/com.google.firebase.quickstart.firebasestorage D/Storage#MainActivity: uploadFromUri:dst:/photos/4b68b0e9-c950-4cf1-bee3-2bedfcda52dd.jpg
07-23 03:15:57.116 23429-23429/com.google.firebase.quickstart.firebasestorage W/UploadTask: could not retrieve file size for upload content://com.google.firebase.quickstart.firebasestorage.fileprovider/external/4b68b0e9-c950-4cf1-bee3-2bedfcda52dd.jpg
07-23 03:15:57.116 23429-23429/com.google.firebase.quickstart.firebasestorage E/UploadTask: could not locate file for uploading:content://com.google.firebase.quickstart.firebasestorage.fileprovider/external/4b68b0e9-c950-4cf1-bee3-2bedfcda52dd.jpg
07-23 03:15:57.126 23429-23429/com.google.firebase.quickstart.firebasestorage E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
07-23 03:15:57.126 23429-23429/com.google.firebase.quickstart.firebasestorage E/StorageException: No such file or directory
java.io.FileNotFoundException: No such file or directory
at android.os.Parcel.openFileDescriptor(Native Method)
at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:253)
at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:199)
at android.support.v4.content.FileProvider.openFile(FileProvider.java:522)
at android.content.ContentProvider.openAssetFile(ContentProvider.java:1336)
at android.content.ContentProvider.openTypedAssetFile(ContentProvider.java:1516)
at android.content.ContentProvider.openTypedAssetFile(ContentProvider.java:1582)
at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:393)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1088)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:927)
at android.content.ContentResolver.openInputStream(ContentResolver.java:652)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.google.firebase.quickstart.firebasestorage.MainActivity$override.uploadFromUri(MainActivity.java:186)
at com.google.firebase.quickstart.firebasestorage.MainActivity$override.onActivityResult(MainActivity.java:161)
at com.google.firebase.quickstart.firebasestorage.MainActivity$override.access$dispatch(MainActivity.java)
at com.google.firebase.quickstart.firebasestorage.MainActivity.onActivityResult(MainActivity.java:0)
at android.app.Activity.dispatchActivityResult(Activity.java:6549)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4054)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3380)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3425)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2763)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4462)
at android.app.ActivityThread.access$1000(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:1)
您需要创建一个新文件并将fileUri.toString()作为参数传递:
// [START upload_from_uri]
private void uploadFromUri(Uri fileUri) {
Uri uploadUri = Uri.fromFile(new File(fileUri.toString()));
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
// [START get_child_ref]
// Get a reference to store file at photos/<FILENAME>.jpg
final StorageReference photoRef = mStorageRef.child("photos").child(uploadUri.getLastPathSegment());
// [END get_child_ref]
// Upload file to Firebase Storage
// [START_EXCLUDE]
showProgressDialog();
// [END_EXCLUDE]
Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
photoRef.putFile(uploadUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Upload succeeded
Log.d(TAG, "uploadFromUri:onSuccess");
// Get the public download URL
mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
// [START_EXCLUDE]
hideProgressDialog();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Upload failed
Log.w(TAG, "uploadFromUri:onFailure", exception);
mDownloadUrl = null;
// [START_EXCLUDE]
hideProgressDialog();
Toast.makeText(MainActivity.this, "Error: upload failed",
Toast.LENGTH_SHORT).show();
updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
});
}
// [END upload_from_uri]
这是一个有效的方法:
RsaPrivateCrtKeyParameters key;
using (StringReader sr = new StringReader(fbPrivateKey)) //here used StringReader
{
PemReader pr = new PemReader(sr);
key = (RsaPrivateCrtKeyParameters)pr.ReadObject(); //no more keyPair because I just have the privKey only
}
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters(key);
RSACryptoServiceProvider rsa;
rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParams);
var nRsa = PemKeyUtils.GetRSAProviderFromPemFile(fbPrivateKey);
Microsoft.IdentityModel.Tokens.RsaSecurityKey _signingKey = new Microsoft.IdentityModel.Tokens.RsaSecurityKey(nRsa);
Microsoft.IdentityModel.Tokens.SigningCredentials signingCredentials =
new Microsoft.IdentityModel.Tokens.SigningCredentials(_signingKey, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256);
答案 1 :(得分:0)
您的错误非常明确:
java.io.FileNotFoundException: No such file or directory
如果您尝试从galery打开图像以发送FirebaseStorage。验证你的Uri是否属实。