本文整理汇总了Java中android.provider.MediaStore类的典型用法代码示例。如果您正苦于以下问题:Java MediaStore类的具体用法?Java MediaStore怎么用?Java MediaStore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MediaStore类属于android.provider包,在下文中一共展示了MediaStore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getImgInfo
import android.provider.MediaStore; //导入依赖的package包/类
/**
* 获取本地图片信息
* @param context
* @param map
*/
public static void getImgInfo(Context context, HashMap<String, ArrayList<String>> map) {
//获取图片信息表
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = context.getContentResolver();
Cursor mCursor = mContentResolver.query(imageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_TAKEN + " DESC");
while (mCursor.moveToNext()) {
String imgPath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
//获取图片父路径
String parentPath = new File(imgPath).getParentFile().getName();
if (!map.containsKey(parentPath)) {
ArrayList<String> childList = new ArrayList<String>();
childList.add(imgPath);
map.put(parentPath, childList);
} else {
map.get(parentPath).add(imgPath);
}
}
mCursor.close();
}
示例2: checkForDuplicateImage
import android.provider.MediaStore; //导入依赖的package包/类
/**
* Used to find out if we are in a situation where the Camera Intent adds to images
* to the content store. If we are using a FILE_URI and the number of images in the DB
* increases by 2 we have a duplicate, when using a DATA_URL the number is 1.
*
* @param type FILE_URI or DATA_URL
*/
private void checkForDuplicateImage(int type) {
int diff = 1;
Uri contentStore = whichContentStore();
Cursor cursor = queryImgDB(contentStore);
int currentNumOfImages = cursor.getCount();
if (type == FILE_URI && this.saveToPhotoAlbum) {
diff = 2;
}
// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast();
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
if (diff == 2) {
id--;
}
Uri uri = Uri.parse(contentStore + "/" + id);
this.cordova.getActivity().getContentResolver().delete(uri, null, null);
cursor.close();
}
}
示例3: takePhoto
import android.provider.MediaStore; //导入依赖的package包/类
private void takePhoto() {
Intent intentPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ComponentName componentName = intentPhoto.resolveActivity(getPackageManager());
if (componentName != null) {
// 使用 lambda 表达式捕获局部变量,避免了将 tmpFile 作为类变量。
File tmpFile = getTempFile(FileType.IMG);
if (tmpFile != null) {
BuilderUtil.createBuilder(this, intentPhoto)
.asIntent()
.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
.putExtra(MediaStore.EXTRA_OUTPUT, getUri(componentName, tmpFile))
.asBuilder()
.forOk((context, intent) -> context.showPicture(tmpFile))
.start();
}
}
}
示例4: cropImage
import android.provider.MediaStore; //导入依赖的package包/类
/**
* 裁剪图片
*
* @param uri
*/
private void cropImage(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 图片处于可裁剪状态
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 4);
intent.putExtra("aspectY", 3);
// 是否缩放
intent.putExtra("scale", true);
// 设置图片的大小,提高头像的上传速度
intent.putExtra("outputX", 500);
intent.putExtra("outputY", 500);
// 以Uri的方式传递照片
File cropFile = new File(getFilePath() + "crop.jpg");
cropFileUri = Uri.fromFile(cropFile);
// 把裁剪好的图片保存到这个路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropFileUri);
startActivityForResult(intent, CROP_REQUEST);
}
示例5: catchIntent
import android.provider.MediaStore; //导入依赖的package包/类
private void catchIntent(){
if (file == null)
throw new NullPointerException("You need to set the file you want to save the image in.");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
selectedFilePath = file.getPath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
if (activity==null)
return;
// start the image capture Intent
if (dialogFragment != null)
activity.startActivityFromFragment(dialogFragment, intent, requestCode);
else if (fragment!= null)
activity.startActivityFromFragment(fragment, intent, requestCode);
else activity.startActivityForResult(intent, requestCode);
}
示例6: openCamera
import android.provider.MediaStore; //导入依赖的package包/类
public void openCamera() {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = AndroidUtilities.generatePicturePath();
if (image != null) {
if (Build.VERSION.SDK_INT >= 24) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(parentFragment.getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image));
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
}
currentPicturePath = image.getAbsolutePath();
}
parentFragment.startActivityForResult(takePictureIntent, 13);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
示例7: loadInBackground
import android.provider.MediaStore; //导入依赖的package包/类
@Override
public Cursor loadInBackground() {
Cursor albums = super.loadInBackground();
MatrixCursor allAlbum = new MatrixCursor(COLUMNS);
int totalCount = 0;
String allAlbumCoverPath = "";
if (albums != null) {
while (albums.moveToNext()) {
totalCount += albums.getInt(albums.getColumnIndex(COLUMN_COUNT));
}
if (albums.moveToFirst()) {
allAlbumCoverPath = albums.getString(albums.getColumnIndex(MediaStore.MediaColumns.DATA));
}
}
allAlbum.addRow(new String[]{Album.ALBUM_ID_ALL, Album.ALBUM_ID_ALL, Album.ALBUM_NAME_ALL, allAlbumCoverPath,
String.valueOf(totalCount)});
return new MergeCursor(new Cursor[]{allAlbum, albums});
}
示例8: getPathOfImage
import android.provider.MediaStore; //导入依赖的package包/类
public static String getPathOfImage(Context context, Uri uri) throws URISyntaxException {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
} else return null;
} else return null;
}
示例9: instantiateItem
import android.provider.MediaStore; //导入依赖的package包/类
public Object instantiateItem(ViewGroup container, int position){
View v = inflater.inflate(R.layout.viewpager_image,null);
ImageView imgView = (ImageView) v.findViewById(R.id.viewPagerImage);
//이미지를 가져옴
Uri uri = Uri.fromFile(new File(cards.get(position)));
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(inflater.getContext().getContentResolver(),uri);
int nh = (int) (bitmap.getHeight() * (1024.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 1024, nh, true);
imgView.setImageBitmap(scaled);
} catch (IOException e) {
e.printStackTrace();
}
container.addView(v);
return v;
}
示例10: captureImage
import android.provider.MediaStore; //导入依赖的package包/类
/**
* Start camera intent
* Create a temporary file and pass file Uri to camera intent
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File imageFile = ImageUtils.createImageFile(imageDirectory);
if (imageFile != null) {
String authority = getPackageName() + ".fileprovider";
Uri uri = FileProvider.getUriForFile(this, authority, imageFile);
currentImagePath = "file:" + imageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, Constants.REQUEST_CODE_CAPTURE);
} else {
Toast.makeText(this, getString(R.string.error_create_image_file), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, getString(R.string.error_no_camera), Toast.LENGTH_LONG).show();
}
}
示例11: captureVideo
import android.provider.MediaStore; //导入依赖的package包/类
public void captureVideo(Activity activity, final ModuleResultListener listener) {
if (mIsTakingVideo) {
listener.onResult(Util.getError(Constant.CAMERA_BUSY, Constant.CAMERA_BUSY_CODE));
return;
}
mCaptureVideoStartTime = System.currentTimeMillis();
mIsTakingVideo = true;
String fileName = "nat_video_" + new Date().getTime() + ".mov";
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
try {
videoFile = Util.getFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.fromFile(videoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, Constant.VIDEO_REQUEST_CODE);
}
示例12: handleImageOnKitKat
import android.provider.MediaStore; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
public void handleImageOnKitKat(Intent data) {
String imagePath=null;
Uri uri=data.getData();
if (DocumentsContract.isDocumentUri(this,uri)){
//如果是document类型的Uri,则通过document id处理
String docId=DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id=docId.split(":")[1];//解析数字可是的id
String selection=MediaStore.Images.Media._ID+"="+id;
imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri= ContentUris.withAppendedId(Uri.parse("content://downloads/punlic_downloads"),Long.valueOf(docId));
imagePath=getImagePath(contentUri,null);
}
}else if ("content".equalsIgnoreCase(uri.getScheme())){
//如果是content类型的Uri,则使用普通方法
imagePath=getImagePath(uri,null);
}else if ("file".equalsIgnoreCase(uri.getScheme())){
imagePath=uri.getPath();
}
displayImage(imagePath);//根据图片路径显示图片
}
示例13: captureImage
import android.provider.MediaStore; //导入依赖的package包/类
public void captureImage(Activity activity, final ModuleResultListener listener){
if (mIsTakingPhoto) {
listener.onResult(Util.getError(Constant.CAMERA_BUSY, Constant.CAMERA_BUSY_CODE));
return;
}
mCaptureImgStartTime = System.currentTimeMillis();
mIsTakingPhoto = true;
String fileName = "nat_img_" + new Date().getTime() + ".jpg";
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
try {
finalImageFile = Util.getFile(fileName);
} catch (IOException e) {
e.printStackTrace();
listener.onResult(Util.getError(Constant.CAMERA_INTERNAL_ERROR, Constant.CAMERA_INTERNAL_ERROR_CODE));
}
Uri uri = Uri.fromFile(finalImageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(intent, Constant.IMAGE_REQUEST_CODE);
}
示例14: dispatchTakePictureIntentTest3
import android.provider.MediaStore; //导入依赖的package包/类
private void dispatchTakePictureIntentTest3() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(false);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.ge.droid.takingphotossimply.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PICTURE_TEST_3);
}
}
}
示例15: getRealPathFromURI
import android.provider.MediaStore; //导入依赖的package包/类
public static String getRealPathFromURI(Context context, Uri contentURI) throws Exception {
String result;
String[] proj = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = context.getContentResolver().query(contentURI, proj, null, null, null);
if (cursor == null || cursor.getCount() == 0) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
result = cursor.getString(idx);
if (result == null) {
result = contentURI.getPath();
}
cursor.close();
}
return result;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}