ContentProviderによるマルチメディアコンテンツの管理
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:内部機器に格納されたオーディオファイル
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>