数据库发送的火力地堡在android系统不能正常工作,如果我收我的应用程序,火力错误
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final String ANONYMOUS = "anonymous";
public static final int RC_SIGN_IN = 1;
private static final int RC_PHOTO_PICKER = 2;
private String mUsername;
// Firebase instance variables
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mMessagesDatabaseReference;
private ChildEventListener mChildEventListener;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private FirebaseStorage mFirebaseStorage;
private StorageReference mChatPhotosStorageReference;
private SeekBar seekBar;
private RecyclerView recyclerView;
private FloatingActionButton floatingActionButton;
NotificationCompat.Builder notificationBuilder;
VideoAdapter videoAdapter;
List<Video> videoList;
NotificationManager notificationManager;
AlertDialog.Builder alertDialog;
EditText input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsername = ANONYMOUS;
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingactionbutton);
videoList = new ArrayList();
// Initialize Firebase components
mFirebaseDatabase = FirebaseDatabase.getInstance();
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseStorage = FirebaseStorage.getInstance();
seekBar = (SeekBar) findViewById(R.id.seekbar);
mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("videomessages");
mChatPhotosStorageReference = mFirebaseStorage.getReference().child("videos");
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
onSignedInInitialize(user.getDisplayName());
} else {
// User is signed out
onSignedOutCleanup();
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(false)
.setProviders(
AuthUI.EMAIL_PROVIDER,
AuthUI.GOOGLE_PROVIDER)
.build(),
RC_SIGN_IN);
}
}
};
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
});
attachDatabaseReadListener();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
// Sign-in succeeded, set up the UI
Toast.makeText(this, "Signed in!", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
// Sign in was canceled by the user, finish the activity
Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
finish();
}
} else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
final Uri selectedImageUri = data.getData();
String uriString = selectedImageUri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
String displayName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getApplicationContext().getContentResolver().query(selectedImageUri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
}
alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Upload");
alertDialog.setMessage("Enter Name");
input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setText(displayName);
alertDialog.setView(input);
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new MyAsyncTask().execute(selectedImageUri);
}});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}
@Override
protected void onResume() {
super.onResume();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onPause() {
super.onPause();
if (mAuthStateListener != null) {
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
}
private void onSignedInInitialize(String username) {
mUsername = username;
attachDatabaseReadListener();
}
private void onSignedOutCleanup() {
mUsername = ANONYMOUS;
}
private void attachDatabaseReadListener() {
mMessagesDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
videoList.clear();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Video postSnapshotValue = postSnapshot.getValue(Video.class);
if (!videoList.contains(postSnapshotValue)) {
videoList.add(postSnapshotValue);
Log.i(TAG, "onDataChange: " + videoList);
}
}
videoAdapter = new VideoAdapter(videoList, MainActivity.this);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(videoAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public class MyAsyncTask extends AsyncTask<Uri, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(final Uri... params) {
final StorageReference photoRef = mChatPhotosStorageReference.child(params[0].getLastPathSegment());
alertDialog.setView(input);
photoRef.putFile(params[0])
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
// progressBar.setVisibility(View.VISIBLE);
Uri downloadUrl = taskSnapshot.getDownloadUrl();
//String nameUrl=taskSnapshot.getMetadata().getName();
Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
Log.i(TAG, "onSuccess: Video Uploaded");
mMessagesDatabaseReference.push().setValue(video);
// mMessagesDatabaseReference.push().setValue(video.getVideoUrl());
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
int progress = (int) ((100 * taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount());
seekBar.setProgress(progress);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Upload in progress")
.setContentIntent(contentIntent(getApplicationContext()))
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
}
notificationManager = (NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
for (int incr = progress; incr <= 100; incr += 5) {
notificationBuilder.setProgress(100, progress, false);
notificationManager.notify(20, notificationBuilder.build());
}
if(progress>=100){
notificationBuilder.setContentText("Upload complete").setProgress(0, 0, false);
notificationManager.notify(20, notificationBuilder.build());
}
}
});
return null;
}
}
private PendingIntent contentIntent(Context context) {
Intent startActivityIntent = new Intent(context, MainActivity.class);
return PendingIntent.getActivity(
context,
0,
startActivityIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
我上传浮动行动button.The视频的点击一个视频火力存储被上传到存储successfully.When视频上传我用FOLL代码数据库发送的火力地堡在android系统不能正常工作,如果我收我的应用程序,火力错误
Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
Log.i(TAG, "onSuccess: Video Uploaded");
mMessagesDatabaseReference.push().setValue(video);
推视频名称和它的下载网址数据库我想这是在firebase
一个错误。我是不知道,但请听我说完。 第一种情况: 当我上传我的视频到firebase
,并保持我的应用程序打开,直到视频上传,视频名称和下载url
被推送到firebase
数据库和视频上传到存储。 第二种情况: 如果我将视频上传到firebase
存储并打开其他应用,例如youtube
,并且在我的通知中,我会收到视频上传量和上传完成视频的通知后,我是否再次打开我的应用,firebase
不会将名称和视频网址推送到数据库,但视频会上传到存储。 为什么这种行为可有人请指导我..
更新:
您正在使用photoRef.putFile(params[0])
活动范围的成功监听。 The documentation解释说:
监听器将在的onStop被自动删除()
当你的活动去的背景下,它被停止并删除侦听器。使用其他形式的addOnSuccessListener()
,它不会将Activity作为第一个参数。
给嫌疑人添加完成侦听器setValue()
。另外,请记录input
的值。我不确定活动停止时UI对象的状态会发生什么。
Video video = new Video(input.getText().toString().trim(),downloadUrl.toString());
Log.i(TAG, "onSuccess: Video Uploaded= " + input.getText());
mMessagesDatabaseReference.push().setValue(video, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
Log.d(TAG, "onComplete: SUCCESS");
} else {
Log.e(TAG, "onComplete: FAILED ", databaseError.toException());
}
}
});
根据对评论的回复,听起来好像您的活动在投放到后台时可能会被破坏(而非停止)。添加这种方法和日志声明你的活动,看看是否是发生:
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
@BobSynder在我的项目中添加代码后,同样的事情发生。如果我开始上传,保持我的应用程序打开,我可以看到Log.i(标签,“onSuccess:Video Uploaded =”+ input.getText()) ;,上传视频并将值推送到数据库。如果我在上传时关闭了我的应用程序,但我无法看到任何日志语句,但视频已上传到Firebase存储但未将值推入数据库 – Pritish
当您说“关闭我的应用程序“你的意思是你把它放在后台,例如进入主屏幕?在这种情况下,您应该继续查看应用程序的日志输出。您在之前的回复中指出,当应用程序关闭时,您会看到“onSuccess:Video Uploaded”日志消息。是对的吗? –
“关闭我的应用程序”意味着我把它放在后台或我正在关闭我的应用程序。我无法看到onSuccess:视频已上传,如果应用程序放在后台或关闭。 – Pritish
对于第二(失败)的情况下,你看到的logcat消息'的onSuccess:视频Uploaded'? –
@BobSnyder是的。 – Pritish