gpt4 book ai didi

java - android setOnClickListener (v) 无法解析 'v'

转载 作者:行者123 更新时间:2023-12-01 08:51:03 25 4
gpt4 key购买 nike

我是 Android 新手,我正在尝试让监听器同时录制视频和捕获图片。它一直给我一个无法解析“v”错误,而且我在时间戳方面也遇到了麻烦,无法让它识别日期。我尝试过切换和启用不同的 API 10、23 和 25。此版本的应用程序当前正在运行 23,以尝试解决我的监听器问题。任何帮助将不胜感激。

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;

import static android.R.attr.data;

public class MainActivity extends Activity
{ //activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

//directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; //store image video file

private ImageView imgPreview;
private VideoView videoPreview;
private Button btnCapturePicture, btnRecordVideo;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imgPreview = (ImageView) findViewById(R.id.imgPreview);
videoPreview = (VideoView) findViewById(R.id.videoPreview);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);

//capture image button click event
btnCapturePicture.setOnContextClickListener(View v)
{
//capture picture
captureImage();
}
btnRecordVideo.setOnClickListener(View v)
{
RecordVideo();
}

if (isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesent support camera",
Toast.LENGTH_LONG).show();
//will closethe app if the device doesent have camera
finish();
}
}

}
//checking device for camera
private boolean isDeviceSupportCamera()
{
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA))
{
//THIS DEVICE HAS A CAMERA
return true;
} else {
//no camera on this device
return false;
}
}

//capturing camera image will launch amera app request image capture
private void captureImage()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

//start the image capture intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
// HERE WE store the file uri as it will be null after returning from the app

@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onRestoreInstanceState(outState);

//save file uri in bundle as it will be null on screen orientation changes
outState.putParcelable("file_uri", fileUri);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);

//get the file uri
fileUri = savedInstanceState.getParcelable("file_uri");
}

//recording video
private void RecordVideo()
{
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
//set video quality
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

//start the video capture intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

//Recieving activity result method will be called after closing the camera
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{ //IF THE RESULT IS CAPTURING IMAGE
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
{
if (resultCode == RESULT_OK)
{
//successfully captured the image, displayit in an image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED)
{
//user cancelled Image Capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT).show();
} else {
//failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
}

} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE)
{
if (requestCode == RESULT_OK)
{ //VIDEO SUCCESSFULLY RECORDED
//prieview the recorded video
previewVideo();
} else if (resultCode == RESULT_CANCELED)
{//user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT).show();
} else {//failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT).show();
}
}
}

//display image from a path to imageView
private void previewCapturedImage()
{//hide video preview
try{
//hide video preview
videoPreview.setVisibility(View.GONE);
imgPreview.setVisibility(View.VISIBLE);

BitmapFactory.Options options = new BitmapFactory.Options();

//downsizing image as it throws outof memory exception for larger images
options.inSampleSize = 8;

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);

imgPreview.setImageBitmap(bitmap);
}

catch(NullPointerException e)
{
e.printStackTrace();
}
}
// previewing recorded video
private void previewVideo()
{
try {
//hide image preview
imgPreview.setVisibility(View.GONE);
videoPreview.setVideoPath(fileUri.getPath());
//start p;aying
videoPreviewStart();
} catch (Exception e)
{
e.printStackTrace();
}
}

private void videoPreviewStart()
{

}

//Helper Methods
//Creating file uri to store image/video
public Uri getOutputMediaFileUri(int type)
{
return Uri.fromFile(getOutputMediaFile(type));
}
//returning image/video
private static File getOutputMediaFile(int type)
{
//external sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
IMAGE_DIRECTORY_NAME);
//create the storage directory if it does not exist
if (!mediaStorageDir.exists())
{
if(!mediaStorageDir.mkdir())
{
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
//Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_IMAGE)
{
mediaFile = new File (mediaStorageDir.getPath() + File.separator
+ "IMG" + timeStamp + ".jpg");
}
else if (type == MEDIA_TYPE_VIDEO)
{
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID" + timeStamp + ".mp4");
}
else
{
return null;
}
return mediaFile;
}

}

最佳答案

问题是这些行:

btnRecordVideo.setOnClickListener(View v) {
RecordVideo();
}

您正在传递一个值,而不是创建一个方法,所以它应该是

btnRecordVideo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//you do your actions here
recordVideo(); //Note: only classes start with capitals, not methods
}
})

您还必须更改犯过类似错误的其他行

关于java - android setOnClickListener (v) 无法解析 'v',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com