大佬教程收集整理的这篇文章主要介绍了Android:如何在ImageView中显示视频缩略图?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
此代码适用于拍摄图像并显示图像.视频也会被录制并保存到手机中,但不会显示在ImageView中.有关如何修改我的代码以使视频出现在ImageView中的任何建议?
除了:RESULT_LOAD_VID部分之外,一切似乎都能正常工作,它应该将选定或录制的视频显示到ImageView.
我收到的错误是:“SkImageDecoder :: Factory返回null”
据我所知,这意味着无论出于何种原因,所选/录制的视频位置都没有传递到RESULT_LOAD_VID部分.
任何帮助表示赞赏.
这是我目前的代码:
public class Media extends AppCompatActivity{ private static int RESULT_LOAD_IMG = 1; private static int RESULT_LOAD_VID = 1; String imgDecodableString; private String SELEctedImagePath = ""; private static final int CAPTURE_VIDEO_ACTIVITY_requEST_CODE = 200; final privatE int CAPTURE_IMAGE = 2; private Uri fileUri; private ImageView mImageView; Toolbar toolbar; private String imgPath; @Override public Boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.toolbarmedia,menu); return true; } @Override public Boolean onOptionsItemSELEcted(MenuItem item) { switch (item.getItemId()) { case R.id.camera: AlertDialog.builder builder = new AlertDialog.builder(Media.this); // builder.settitle("Choose Image source"); builder.setItems(new CharSequence[] { "Take a Photo","Choose from Gallery" },new DialogInterface.onClickListener() { @Override public void onClick(DialogInterface dialog,int which) { switch (which) { case 0: Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent1.putExtra(MediaStore.EXTRA_OUTPUT,setImageUri()); startActivityForResult(intent1,CAPTURE_IMAGE); break; case 1: // CreatE intent to Open Image applications like Gallery,Google Photos Intent galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent,RESULT_LOAD_IMG); break; default: break; } } }); builder.show(); return true; case R.id.video: AlertDialog.builder builder2 = new AlertDialog.builder(Media.this); // builder.settitle("Choose Image source"); builder2.setItems(new CharSequence[]{"Take a Video","SELEct Video from Phone"},int which) { switch (which) { case 0: //create new Intent Intent intent_video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video intent_video.putExtra(MediaStore.EXTRA_OUTPUT,fileUri); // set the image file name intent_video.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); // set the video image quality to high // start the Video CapturE intent startActivityForResult(intent_video,CAPTURE_VIDEO_ACTIVITY_requEST_CODE); break; case 1: // CreatE intent to Open Image applications like Gallery,android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); startActivityForResult(galleryIntent,RESULT_LOAD_VID); break; default: break; } } }); builder2.show(); return true; case R.id.mic: return true; default: // If we got here,the user's action was not recognized. // Invoke the superclass to handle it. return super.onOptionsItemSELEcted(item); } } @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_media); toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); mImageView = (ImageView) findViewById(R.id.media_display); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); try { // When an Image is picked if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) { // Get the Image from data Uri SELEctedImage = data.getData(); String[] filePathcolumn = {MediaStore.Images.Media.DATA}; // Get the cursor cursor cursor = getContentResolver().query(SELEctedImage,filePathcolumn,null,null); // Move to first row cursor.moveToFirst(); int columnIndex = cursor.getcolumnIndex(filePathcolumn[0]); imgDecodableString = cursor.getString(columnIndeX); cursor.close(); ImageView imgView = (ImageView) findViewById(R.id.media_display); // Set the Image in ImageView after decoding the String imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString)); } else if (requestCode == CAPTURE_IMAGE) { SELEctedImagePath = getImagePath(); System.out.println("path" + SELEctedImagePath); mImageView.setImageBitmap(decodeStream(SELEctedImagePath)); } else if(requestCode == RESULT_LOAD_VID && resultCode == RESULT_OK && null != data){ // Get the Image from data Uri SELEctedVideo = data.getData(); String[] filePathcolumn = {MediaStore.Video.Media.DATA}; //Get the cursor cursor cursor = getContentResolver().query(SELEctedVideo,null); // Move to first row cursor.moveToFirst(); int columnIndex = cursor.getcolumnIndex(filePathcolumn[0]); imgDecodableString = cursor.getString(columnIndeX); cursor.close(); ImageView vidView = (ImageView) findViewById(R.id.media_display); // Set the Image in ImageView after decoding the String vidView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString)); }else if (requestCode == CAPTURE_VIDEO_ACTIVITY_requEST_CODE) { if (resultCode == RESULT_OK) { // Video captured and saved to fileUri specified in thE intent Toast.makeText(this,"Video Saved to Phone",Toast.LENGTH_LONG).show(); } else if (resultCode == RESULT_CANCELED) { // User cancelled the video capture } else { // Video capture Failed,advise user Toast.makeText(this,"Video capture Failed.",Toast.LENGTH_LONG).show(); super.onActivityResult(requestCode,data); } } } catch (Exception E) { Toast.makeText(this,"Something went wrong",Toast.LENGTH_LONG) .show(); } } public static final int MEDIA_TYPE_IMAGE = 1; public static final int MEDIA_TYPE_VIDEO = 2; /** Create a file Uri for saving an image or video */ private static Uri getOutputMediaFileUri(int typE){ return Uri.fromFile(getOutputMediaFile(typE)); } /** Create a File for saving an image or video */ private static File getOutputMediaFile(int typE){ // To be safe,you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES),"MyCameraApp"); // This LOCATIOn works best if you want the created images to be shared // @R_618_7000@n applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ Log.d("MyCameraApp","Failed to create directory"); return null; } } // Create a media file name String timestamp = new SimpleDateFormat("yyyymMdd_HHmmss").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; } public Uri setImageUri() { File file = new File(Environment.getExternalStorageDirectory(),"image" + new Date().getTime() + ".png"); Uri imguri = Uri.fromFile(filE); this.imgPath = file.getAbsolutePath(); return imguri; } public String getImagePath() { return imgPath; } public Bitmap decodeStream(String path) { try { // Decode image size BitmapFactory.options o = new BitmapFactory.options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(path,o); // The new size we want to scale to final int required_SIZE = 70; // Find the correct scale value. It should be the power of // 2. int scale = 1; while (o.outWidth / scale / 2 >= required_SIZE && o.outHeight / scale / 2 >= required_SIZE) scale *= 2; // Decode with inSampleSize BitmapFactory.options o2 = new BitmapFactory.options(); o2.inSampleSize = scale; return BitmapFactory.decodeFile(path,o2); } catch (Throwable E) { e.printStackTrace(); } return null; } }
以上是大佬教程为你收集整理的Android:如何在ImageView中显示视频缩略图?全部内容,希望文章能够帮你解决Android:如何在ImageView中显示视频缩略图?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。