مدیریت عکس و فیلم و صدا و فایل های مختلف در اندروید
پنجشنبه 12 آذر 1394در این مقاله می خواهیم در مورد مدیریت عکس و فیلم و صدا و دوربین و میکروفون و هر فایلی که در دستگاه های اندروید استفاده می شود را صحبت نماییم.و در مورد مجوز دسترسی آن ها صحبت کنیم.
برای مدیریت عکس ها و مدیریت حافظه ی داخلی :
استفاده ازIntent.ACTION_GET_CONTENT که به کاربر اجازه می دهد که عکس و یا مدیریت فایل را انتخاب نماید.در این قسمت نیاز به مجوز ندارد.
نمونه کد آن به صورت زیر است:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_CHOOSER = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_GET_CONENT);
intent.setType("image/*");
this.startActivityForResult(intent, REQ_CHOOSER);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CHOOSER:
if (resultCode == Activity.RESULT_OK) {
/* Next you can show selected image in ImageView */
imageView1.setImageURI(data.getData());
} else {
Toast.makeText(this,
"Canceled by user, no photo selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
این کد از داخل file manager به عکس ها دسترسی خواهد داشت.
گالری:
Intent.ACTION_PICKانتخاب عکس به صورت مستقیم از گالری تصاویر، که نیاز به مجوز دسترسی ندارد.
نمونه کد به صورت زیر خواهد بود:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_GALLERY = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
this.startActivityForResult(intent, REQ_FROM_GALLERY);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK) {
/* Next you can show selected image in ImageView */
imageView1.setImageURI(data.getData());
} else {
Toast.makeText(this,
"Canceled by user, no photo selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
برای دوربین:
برای استفاده از دوربین سفارشی و عکس گرفتن و ذخیره کردن عکس داخل حافظه ی گوشی باید از مجوز های دسترسی برای استفاده از دوربین و استفاده از ذخیره کردن حافظه ی داخلی استفاده نماییم:
برای دسترسی به گوشی مجوز دسترسی آن android.permission.CAMERA می شود.
و برای دسترسی به حافظه ی داخلی مجوز دسترسی آن android.permission.WRITE_EXTERNAL_STORAGE می شود.
نمونه کد به صورت زیر خواهد بود:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_CAMERA = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(intent, REQ_FROM_CAMERA);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
/* ------------------ Getting Thumbnail -------------------------
* --------------------------------------------------------------
* Thumbnail is photo miniaturized into small resolution, i.e.:
* 216x324 (HTC Wildfire S), 120x160 (Fly IQ434).
* Thumbnail can be used as an avatar, or as an icon (in UI
* there user at first seeing a list of icons, and next
* selects any photo from list and seeing it in full-size.
* --------------------------------------------------------------
*/
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
/* Next you can show Bitmap in ImageView */
imageView1.setImageBitmap(bmp);
/* ... and get it's size in pixels */
Toast.makeText(this, bmp.getWidth() + "x" +
bmp.getHeight(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Canceled by user, no photo selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
می توانید برای درک بهتر به ایجاد دوربین سفارشی در اندروید مقاله رجوع نمایید.
Video:
برای استفاده از ویدئو که به انتخاب کاربر است یا از گالری و یا از داخل حافظه ی داخلی انتخاب می شود.
که از ACTION_GET_CONTENT. استفاده می شود.
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_GALLERY = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
this.startActivityForResult(intent, REQ_FROM_GALLERY);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK) {
/* Next you can show selected video in VideoView */
videoView1.setVideoURI(data.getData());
videoView1.start();
} else {
Toast.makeText(this,
"Canceled by user, no video selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
برای دسترسی به گالری تصاویر باید ازACTION_PICK. استفاده نمایید.
نمونه کد استفاده از گالری عکس:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_GALLERY = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
this.startActivityForResult(intent, REQ_FROM_GALLERY);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK) {
/* Next you can show selected image in ImageView */
videoView1.setVideoURI(data.getData());
videoView1.start();
} else {
Toast.makeText(this,
"Canceled by user, no video selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
برای ضبط کردن فیلم و استفاده از دوربین می توانید از نمونه کد زیر استفاده نمایید:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_CAMERA = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
this.startActivityForResult(intent, REQ_FROM_CAMERA);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
videoView1.setVideoURI(data.getData());
videoView1.start();
} else {
Toast.makeText(this,
"Canceled by user, no video selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
برای استفاده از گالری قبلا گفته بودیم که از ACTION_PICK استفاده نماییم ولی دیگر این قابل استفاده نیست و ما از ACTION_GET_CONTENT استفاده می نماییم.
نمونه کد آن به صورت زیر است:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_GALLERY = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
this.startActivityForResult(intent, REQ_FROM_GALLERY);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Canceled by user, no audio selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
برای استفاده از میکروفون ازMediaStore.Audio.Media.RECORD_SOUND_ACTION استفاده نمایید.
نمونه کد استفاده از میکروفون به صورت زیر خواهد بود:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_MICRO = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
this.startActivityForResult(intent, REQ_FROM_MICRO);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_MICRO:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Canceled by user, no audio selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
هر فایلی که داخل SD باشد پیشنهاد می کنم که از */* استفاده نشود و به جای آن ازfile/* استفاده شود.
نمونه کد برای استفاده از به صورت زیر است:
/* This constant is just identifier for onActivityResult */
/* Place it inside Activity's class */
final int REQ_FROM_DEFFM = 0;
/* Place it inside any Activity's method where you need show a capturing activity */
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
this.startActivityForResult(intent, REQ_FROM_DEFFM);
/* Place it inside Activity's class */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_FROM_DEFFM:
if (resultCode == Activity.RESULT_OK) {
/* Absolute File Path */
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Canceled by user, no file selected.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
- Android
- 2k بازدید
- 2 تشکر