">
当前位置:   article > 正文

Android ContentProvider管理多媒体内容_content providers create digital text, video, audi

content providers create digital text, video, audio, and graphics to sen

这里写图片描述

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • 1
  • 2

activity_contact_media.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="shortcut.song.com.myapplication.ContactMedia">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="view-media"
        android:onClick="viewMedia"/>
 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD-MEDIA"
        android:onClick="addMedia"/>
    <ListView
        android:id="@+id/show_media"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

contact_media_view.xml

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

    <ImageView
        android:id="@+id/image_contact_media"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

ContactMedia.java

package shortcut.song.com.myapplication;

import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContactMedia extends AppCompatActivity {

    ListView show;
    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_contact_media);
        show = (ListView)findViewById(R.id.show_media);


        show.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                View viewDialog = getLayoutInflater().inflate(R.layout.contact_media_view,null);
                ImageView image = (ImageView)viewDialog.findViewById(R.id.image_contact_media);

                image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(position)));

                new AlertDialog.Builder(ContactMedia.this)
                    .setView(viewDialog)
                    .setPositiveButton("Ok", null)
                    .show();
            }
        });
    }

    public void viewMedia(View v) {
        names.clear();
        descs.clear();
        fileNames.clear();
        //通过ContentResolver查询所有图片信息
        Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null,null);
        while (cursor.moveToNext()) {
            // 获取图片的显示名字
            String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME));

            // 获取图片的详细描述
            String desc = cursor.getString(cursor.getColumnIndex(Media.DESCRIPTION));

            // 获取图片的保存位置的数据
            byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA));
            names.add(name);
            descs.add(desc);
            fileNames.add(new String(data, 0, data.length - 1));
        }

        List<Map<String, Object>> listItems = new ArrayList<>();

        for (int i = 0; i < names.size(); i++) {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("name", names.get(i));
            listItem.put("desc", descs.get(i));
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(
                ContactMedia.this,listItems
                ,R.layout.contactmedia_line, new String[]{"name", "desc"}
                ,new int[]{R.id.name, R.id.desc});

            show.setAdapter(simpleAdapter);


    }

public void addMedia(View v) {
        ContentValues values = new ContentValues();
        values.put(Media.DISPLAY_NAME, "qiao");
        values.put(Media.DESCRIPTION, "大桥");
        values.put(Media.MIME_TYPE, "image/jpeg");

        //插入数据,返回所插入数据对应的URI
        Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
        // 加载应用程序下的qiao图片
        Bitmap bitmap = BitmapFactory.decodeResource(ContactMedia.this.getResources(), R.drawable.qiao);
        OutputStream os = null;
        try {
            // 获取刚插入的数据的Uri对应的输出流
            os = getContentResolver().openOutputStream(uri);
            //将bitmap图片保存到Uri对应的数据节点
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

运行效果:

这里写图片描述
这里写图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/271942
推荐阅读
相关标签
  

闽ICP备14008679号