版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android如何選擇圖片或視頻進(jìn)行循環(huán)播放
private
void
setVideoPath()
{
Intent
intent
=
new
Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//設(shè)置類型,我這里是任意類型,任意后綴的可以這樣寫。
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent,
VIDEO_PATH);
}@Override
public
void
onActivityResult(int
requestCode,
int
resultCode,
Intent
data)
{
if
(resultCode
!=
RESULT_OK)
return;
switch
(requestCode)
{
case
VIDEO_PATH:
Uri
uri
=
data.getData();
String
path
=
getPath(
uri);
showToastReal("你選中的視頻路徑:"
+
path);
SpUtils.getInstace(this).saveString("videoPath",
path);
break;
case
PIC_PATH:
Uri
picUri
=
data.getData();
String
picPath
=
getPath(picUri);
showToastReal("你選中的圖片路徑:"
+
picPath);
SpUtils.getInstace(this).saveString("picPath",
picPath);
break;
}
}public
String
getPath(Uri
uri)
{
String
path;
if
("file".equalsIgnoreCase(uri.getScheme()))
{//使用第三方應(yīng)用打開
path
=
uri.getPath();
return
path;
}
if
(Build.VERSION.SDK_INT
>
Build.VERSION_CODES.KITKAT)
{//4.4以后
path
=
getPath(this,
uri);
}
else
{//4.4以下下系統(tǒng)調(diào)用方法
path
=
getRealPathFromURI(uri);
}
return
path;
}
@SuppressLint("NewApi")
public
String
getPath(final
Context
context,
final
Uri
uri)
{
final
boolean
isKitKat
=
Build.VERSION.SDK_INT
>=
Build.VERSION_CODES.KITKAT;
//
DocumentProvider
if
(isKitKat
&&
DocumentsContract.isDocumentUri(context,
uri))
{
//
ExternalStorageProvider
if
(isExternalStorageDocument(uri))
{
final
String
docId
=
DocumentsContract.getDocumentId(uri);
final
String[]
split
=
docId.split(":");
final
String
type
=
split[0];
if
("primary".equalsIgnoreCase(type))
{
return
Environment.getExternalStorageDirectory()
+
"/"
+
split[1];
}
}
//
DownloadsProvider
else
if
(isDownloadsDocument(uri))
{
final
String
id
=
DocumentsContract.getDocumentId(uri);
final
Uri
contentUri
=
ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return
getDataColumn(context,
contentUri,
null,
null);
}
//
MediaProvider
else
if
(isMediaDocument(uri))
{
final
String
docId
=
DocumentsContract.getDocumentId(uri);
final
String[]
split
=
docId.split(":");
final
String
type
=
split[0];
Uri
contentUri
=
null;
if
("image".equals(type))
{
contentUri
=
MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
else
if
("video".equals(type))
{
contentUri
=
MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
else
if
("audio".equals(type))
{
contentUri
=
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final
String
selection
=
"_id=?";
final
String[]
selectionArgs
=
new
String[]{split[1]};
return
getDataColumn(context,
contentUri,
selection,
selectionArgs);
}
}
//
MediaStore
(and
general)
else
if
("content".equalsIgnoreCase(uri.getScheme()))
{
return
getDataColumn(context,
uri,
null,
null);
}
//
File
else
if
("file".equalsIgnoreCase(uri.getScheme()))
{
return
uri.getPath();
}
return
null;
}
/**
*
Get
the
value
of
the
data
column
for
this
Uri.
This
is
useful
for
*
MediaStore
Uris,
and
other
file-based
ContentProviders.
*
*
@param
context
The
context.
*
@param
uri
The
Uri
to
query.
*
@param
selection
(Optional)
Filter
used
in
the
query.
*
@param
selectionArgs
(Optional)
Selection
arguments
used
in
the
query.
*
@return
The
value
of
the
_data
column,
which
is
typically
a
file
path.
*/
public
String
getDataColumn(Context
context,
Uri
uri,
String
selection,
String[]
selectionArgs)
{
Cursor
cursor
=
null;
final
String
column
=
"_data";
final
String[]
projection
=
{column};
try
{
cursor
=
context.getContentResolver().query(uri,
projection,
selection,
selectionArgs,
null);
if
(cursor
!=
null
&&
cursor.moveToFirst())
{
final
int
column_index
=
cursor.getColumnIndexOrThrow(column);
return
cursor.getString(column_index);
}
}
finally
{
if
(cursor
!=
null)
cursor.close();
}
return
null;
}
/**
*
@param
uri
The
Uri
to
check.
*
@return
Whether
the
Uri
authority
is
ExternalStorageProvider.
*/
public
boolean
isExternalStorageDocument(Uri
uri)
{
return
"com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
*
@param
uri
The
Uri
to
check.
*
@return
Whether
the
Uri
authority
is
DownloadsProvider.
*/
public
boolean
isDownloadsDocument(Uri
uri)
{
return
"viders.downloads.documents".equals(uri.getAuthority());
}
/**
*
@param
uri
The
Uri
to
check.
*
@return
Whether
the
Uri
authority
is
MediaProvider.
*/
public
boolean
isMediaDocument(Uri
uri)
{
return
"viders.media.documents".equals(uri.getAuthority());
}public
class
VideoActivity
extends
BaseActivity
{
@Bind(R.id.sv_ad)
SurfaceView
vv;
@Bind(R.id.id_ig_back)
ImageView
idIgBack;
private
MediaPlayer
mPlayer;
private
String
path;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_video);
ButterKnife.bind(this);
verifyStoragePermissions(this);
init();
//
init2();
}
private
void
init2()
{
String
path
=
SpUtils.getInstace(VideoActivity.this).getString("videoPath");
Uri
uri
=
Uri.parse("file://"
+
path);
try
{
MediaPlayer
mediaPlayer
=
new
MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(),
uri);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
private
void
init()
{
idIgBack.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
finish();
}
});
vv.getHolder().addCallback(new
SurfaceHolder.Callback()
{
@Override
public
void
surfaceDestroyed(SurfaceHolder
holder)
{
if
(mPlayer
!=
null)
{
mPlayer.stop();
mPlayer.release();
mPlayer
=
null;
}
}
@Override
public
void
surfaceCreated(SurfaceHolder
holder)
{
path
=
SpUtils.getInstace(VideoActivity.this).getString("videoPath");
try
{
if
(mPlayer
==
null)
{
mPlayer
=
MediaPlayer.create(VideoActivity.this,
Uri.parse("file://"
+
path));
}
if(mPlayer==null){
showToastReal("請?jiān)趥€(gè)人中心中選擇正確的視頻");
}
mPlayer.setDisplay(holder);//將SurfaceHolder關(guān)聯(lián)mediaplayer
mPlayer.setLooping(true);
mPlayer.start();
mPlayer.setOnErrorListener(new
MediaPlayer.OnErrorListener()
{
@Override
public
boolean
onError(MediaPlayer
mp,
int
what,
int
extra)
{
//
TODO
Auto-generated
method
stub
return
false;
}
});
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
@Override
public
void
surfaceChanged(SurfaceHolder
holder,
int
format,
int
width,
int
height)
{
}
});
}
public
void
onBack(View
view)
{
finish();
}
@Override
public
void
loadNetData()
{
}
private
static
final
int
REQUEST_EXTERNAL_STORAGE
=
1;
private
static
String[]
PERMISSIONS_STORAGE
=
{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
*
檢查應(yīng)用程序是否允許寫入存儲(chǔ)設(shè)備
*
<p>
*
<p>
*
<p>
*
如果應(yīng)用程序不允許那么會(huì)提示用戶授予權(quán)限
*
*
@param
activity
*/
public
static
void
verifyStoragePermissions(Activity
activity)
{
//
Check
if
we
have
wr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024美容院客戶投訴處理協(xié)議
- 2024年銷售代表就業(yè)協(xié)議書3篇
- 二零二五年度現(xiàn)代簡約餐廳裝飾裝修與品牌推廣合同3篇
- 2025年度大米種植與收購金融服務(wù)合同3篇
- 2025年度道路施工安全防護(hù)及交通疏導(dǎo)協(xié)議3篇
- 2024年運(yùn)輸合同之貨物運(yùn)輸路線與時(shí)間保障
- 2025年度智能調(diào)光窗簾系統(tǒng)項(xiàng)目合同書3篇
- 2025年度臨時(shí)運(yùn)輸司機(jī)績效考核及獎(jiǎng)勵(lì)合同4篇
- 2024衣柜墻板吊頂裝修工程款項(xiàng)支付與結(jié)算合同
- 2025年度二零二五廠區(qū)生態(tài)修復(fù)與綠化養(yǎng)護(hù)綜合服務(wù)合同3篇
- 2025年中國高純生鐵行業(yè)政策、市場規(guī)模及投資前景研究報(bào)告(智研咨詢發(fā)布)
- 湖北省黃石市陽新縣2024-2025學(xué)年八年級上學(xué)期數(shù)學(xué)期末考試題 含答案
- 2022-2024年浙江中考英語試題匯編:完形填空(學(xué)生版)
- 2025年廣東省廣州市荔灣區(qū)各街道辦事處招聘90人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 中試部培訓(xùn)資料
- 硝化棉是天然纖維素硝化棉制造行業(yè)分析報(bào)告
- 央視網(wǎng)2025亞冬會(huì)營銷方案
- 北師大版數(shù)學(xué)三年級下冊豎式計(jì)算題100道
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)全套教學(xué)課件
- 屋頂分布式光伏發(fā)電項(xiàng)目施工重點(diǎn)難點(diǎn)分析及應(yīng)對措施
- 胃鏡下超聲穿刺護(hù)理配合
評論
0/150
提交評論