使用ContentProvider管理多媒体内容

本文介绍了如何利用Android的ContentProvider来管理多媒体内容,包括对外部和内部存储的音频、图片及视频文件的访问,提供了如MediaStore.Audio.Media.EXTERNAL_CONTENT_URI等 Uri 示例。
摘要由CSDN通过智能技术生成

android为多媒体提供的Uri:

 1、MediaStore.Audio.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的音频文件

  2、MediaStore.Audio.Mdia.INTERNAL_CONTENT_URI:存储在手机内部上的音频文件

  3、MediaStore.Images.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的图片文件

  4、MediaStore.Images.Mdia.INTERNAL_CONTENT_URI:存储在内部设备上的图片文件

 3、MediaStore.Video.Mdia.EXTERNAL_CONTENT_URI:存储在外部设备上的音频文件

  4、MediaStore.Video.Mdia.INTERNAL_CONTENT_URI:存储在内部设备上的音频文件


package com.example.mediaprovidertest;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button bt1, bt2;
	private ListView list1;

	ArrayList<String> names = new ArrayList<String>();
	ArrayList<String> descs = new ArrayList<String>();
	ArrayList<String> filenames = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt1 = (Button) findViewById(R.id.bt1);
		bt2 = (Button) findViewById(R.id.bt2);
		list1 = (ListView) findViewById(R.id.list);

		bt1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 清空names、desc、fileName集合里原有的数据
				names.clear();
				descs.clear();
				filenames.clear();
				// 通过ContentResolver查询所有图片信息
				Cursor curos = getContentResolver().query(
						Media.EXTERNAL_CONTENT_URI, null, null, null, null);
				while (curos.moveToNext()) {
					// 获取图片显示的名字
					String name = curos.getString(curos
							.getColumnIndex(Media.DISPLAY_NAME));
					// 获取图片的详细信息、
					String desc = curos.getString(curos
							.getColumnIndex(Media.DESCRIPTION));
					// 将图片名保存的位置数据
					byte[] data = curos.getBlob(curos
							.getColumnIndex(Media.DATA));
					// 将图片名添加到names集合中
					names.add(name);
					// 将图片描述添加到desc集合中
					descs.add(desc);
					// 将图片保存路径添加到fileNames集合中
					filenames.add(new String(data, 0, data.length - 1));
				}
				// 创建一个List集合的元素是map
				List<Map<String, Object>> listitems = new ArrayList<Map<String, Object>>();
				// 将names、descs两个集合对象的数据转换到map集合
				for (int i = 0; i < names.size(); i++) {
					Map<String, Object> listitem = new HashMap<String, Object>();
					listitem.put("name", names.get(i));
					listitem.put("desc", descs.get(i));
					listitems.add(listitem);
				}
				SimpleAdapter simple = new SimpleAdapter(MainActivity.this,
						listitems, R.layout.items, new String[] { "name",
								"desc" }, new int[] { R.id.txt1, R.id.txt2 });
				list1.setAdapter(simple);

			}
		});
		list1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 加载view.xml界面布局代表视图
				View view = getLayoutInflater().inflate(R.layout.view, null);
				// 获取viewDialog中ImageView组件
				ImageView image1 = (ImageView) view.findViewById(R.id.image1);
				// 设置image显示指定的图片
				image1.setImageBitmap(BitmapFactory.decodeFile(filenames
						.get(arg2)));
				// 使用对话框显示用户单击的图片
				new AlertDialog.Builder(MainActivity.this).setView(view)
						.setPositiveButton("确定", null).show();

			}
		});
		bt2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 创建ContentValues对象,准备插入数据
				ContentValues values = new ContentValues();
				values.put(Media.DISPLAY_NAME, "jinta");
				values.put(Media.DESCRIPTION, "金塔");
				values.put(Media.MIME_TYPE, "image/jpeg");
				// 插入数据对应的Uri
				Uri uri = getContentResolver().insert(
						Media.EXTERNAL_CONTENT_URI, values);
				// 加载应用程序下的jinta图片
				Bitmap bitmap = BitmapFactory.decodeResource(
						MainActivity.this.getResources(), R.drawable.jinta);
				OutputStream os = null;
				try {
					// 获取刚插入的数据的Uri对应的输出流
					os = getContentResolver().openOutputStream(uri);
					// 将bitmap图片保存到Uri对应的数据节点中
					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
					os.close();
				} catch (IOException io) {
					io.printStackTrace();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <Button 
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看图片"/>
    <Button 
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加图片"/>
    <ListView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>



</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</LinearLayout>



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值