Android初学者求Parse Server相册视频上传完整可行教程
Hey there! Glad you're diving into Android development and Parse Server—let's turn your video picker code into a complete upload workflow. I'll break this down into simple, beginner-friendly steps:
You've already got the intent to pick a video set up, so next we need to catch the result once the user selects a video. In your Activity, override the onActivityResult method (note: if you're using AndroidX, the newer registerForActivityResult API is recommended, but we'll stick with your current approach first):
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); // Check if this is our video pick request and the selection was successful if (requestCode == 1 && resultCode == RESULT_OK && data != null) { Uri selectedVideoUri = data.getData(); // Pass the URI to our upload method uploadVideoToParse(selectedVideoUri); } }
The URI we get from the media picker isn't always a direct file path, so we need a helper method to get the actual path of the video file:
private String getRealVideoPath(Uri videoUri) { String[] projection = {MediaStore.Video.Media.DATA}; Cursor cursor = getContentResolver().query(videoUri, projection, null, null, null); if (cursor == null) { Toast.makeText(this, "Could not access video file", Toast.LENGTH_SHORT).show(); return null; } int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); cursor.moveToFirst(); String videoPath = cursor.getString(columnIndex); cursor.close(); return videoPath; }
Now we'll create a ParseFile from the video, upload it, and optionally attach it to a ParseObject (to keep track of metadata like who uploaded it, video name, etc.):
private void uploadVideoToParse(Uri videoUri) { String videoPath = getRealVideoPath(videoUri); if (videoPath == null) return; File videoFile = new File(videoPath); ParseFile parseVideoFile; try { // Create ParseFile from the video input stream parseVideoFile = new ParseFile(videoFile.getName(), new FileInputStream(videoFile)); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(this, "Failed to read video file", Toast.LENGTH_SHORT).show(); return; } // First upload the raw video file to Parse parseVideoFile.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { // File uploaded successfully! Now attach it to a ParseObject for context ParseObject userVideo = new ParseObject("UserVideos"); userVideo.put("videoFile", parseVideoFile); // Add optional metadata (adjust these to your needs) userVideo.put("uploadedBy", ParseUser.getCurrentUser()); // If you're using user auth userVideo.put("videoName", videoFile.getName()); // Save the object with attached video to Parse Server userVideo.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { Toast.makeText(MainActivity.this, "Video uploaded successfully!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "Failed to save video data: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } } }); } else { Toast.makeText(MainActivity.this, "Failed to upload video: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } } }); }
- Parse Initialization: Don't forget to initialize Parse in your app's
onCreate(either in an Application class or your main Activity):Parse.initialize(new Parse.Configuration.Builder(this) .applicationId("YOUR_APP_ID") .clientKey("YOUR_CLIENT_KEY") // Skip this if your server doesn't use a client key .server("YOUR_PARSE_SERVER_ENDPOINT/parse/") // e.g., "https://your-server.com/parse/" .build()); - Permissions for Android 10+: If you're targeting API 29 or higher, add
android:requestLegacyExternalStorage="true"to your<application>tag inAndroidManifest.xml—this lets the app access the file path using our helper method. - Progress Tracking: For large videos, add a progress indicator using the
ProgressCallback:parseVideoFile.saveInBackground(saveCallback, new ProgressCallback() { @Override public void done(Integer percentDone) { // Update a progress bar or log here, e.g.: Log.d("Upload", "Progress: " + percentDone + "%"); } }); - Error Handling: The toasts are basic—you can expand this with more user-friendly messages or logging for debugging.
内容的提问来源于stack exchange,提问作者user1906431

