The initial stage of ContentProvider - read all pictures in SD card

07-02 01:38:36.910: E/AndroidRuntime(2694): Process: com.jxust.day07_08_contentprovider, PID: 2694
07-02 01:38:36.910:
E/AndroidRuntime(2694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jxust.day07_08_contentprovider
/com.jxust.day07_08_contentprovider.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2694, uid=10069
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

 

Manipulating multimedia data through predefined uri

(1) Overview
Android provides predefined Uri for applications to operate common data, such as video, audio, picture, file, database, etc.


The above data in Android may be stored on the memory card or on the SD card. And it is likely to be stored in different folders. Through Uri, you can first specify which type of data to access, secondly specify whether the data to be accessed is in the memory card or SD card, and finally you can search for qualified data through conditions. There is no need to consider which folder the data is in.


The above way of accessing data provides consistent operations on different types of data, which effectively simplifies programming.

(2) Operation picture data 1. Uri android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URL
for accessing SD card pictures 2. Uri
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URL for accessing SD card pictures 3. MediaStore class MediaStore class It is a common class for Android multimedia development. This class is located in the android.provider package. This class defines internal classes such as Images, Audio, and Video, which are used to represent pictures, audio and video. 4. The Images class The Images class is an internal class of the MediaStore class and is used to represent image data. A set of constants are defined in this class to represent the properties of the image. 5. Constants of Images.Media










1) Images.Media.ID: The id value of the image, which is created by the system.
2) Images.Media.DISPLAY_NAME: The display name of the image.
3) Images.Media.DESCRIPTION: Detailed description of the picture.
4) Images.Media.DATA: The location where the image is saved
5) Images.Media.TITLE: the title of the picture
6) Images.Media.MIME_TYPE: type of image, format: type/subtype
7) Images.Media.SIZE: The space occupied by the image, unit: bytes
8) Images.Media.WIDTH: Image width
9) Images.Media.HEIGHT: Image height

 

6. Thumbnails class
Thumbnails class is used to obtain thumbnails of videos or pictures in the system. This class provides the following methods to get thumbnails:

public static Bitmap getThumbnail(ContentResolver cr,long origId,int kind,BitmapFactory.Option options)
Function: Get the thumbnail of the specified id
Parameters - cr: ContentResolver object
Parameters - origin: the id value of the image
Parameters - kind: the type of thumbnail, there are two constants
Thumbnails.MICRO_KIND: minimum type
Thumbnails.MINI_KIND: Mini type
Parameter - options: BitmapFactory.Options type is used to prevent memory overflow caused by too large pictures. If the overflow problem is not considered, this parameter can be set to null

 
Tip:
Starting from sdk4.4, reading SD card information needs to apply for permission in the project manifest file:
android.permission.READ_EXTERNAL_STORAGE

A small case: reading pictures in SD card

 

 

package com.jxust.day07_08_contentprovider;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		getImagesInfo();
	}

	private void getImagesInfo() {
		ContentResolver resolver = getContentResolver();
		// MediaStore.Images.Media.EXTERNAL_CONTENT_URI allows the user to access the SD card
		
		String[] projection = {
				Images.Media._ID,
				Images.Media.DATA,
				Images.Media.WIDTH,
				Images.Media.HEIGHT,
				Images.Media.SIZE
		};
		Cursor c = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
		while(c.moveToNext()){
			int id = c.getInt(c.getColumnIndex(Images.Media._ID)); // Get the value of ID through the index of the column
			String path = c.getString(c.getColumnIndex(Images.Media.DATA));
			double width = c.getDouble(c.getColumnIndex(Images.Media.WIDTH));
			double height = c.getDouble(c.getColumnIndex(Images.Media.HEIGHT));
			double size = c.getDouble(c.getColumnIndex(Images.Media.SIZE));
			StringBuilder sb = new StringBuilder();
			sb.append("id=").append(id)
			.append(",path=").append(path)
			.append(",width=").append(width)
			.append(",height=").append(height)
			.append(",size=").append(size);
			Log.i("main", sb.toString());
		}
	}
	
	
}

 

 

After running the discovery, there are the following prompt errors in Logcat:

 

07-02 01:38:36.910: E/AndroidRuntime(2694): Process: com.jxust.day07_08_contentprovider, PID: 2694
07-02 01:38:36.910: E/AndroidRuntime(2694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jxust.day07_08_contentprovider/com.jxust.day07_08_contentprovider.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2694, uid=10069 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

 

 

It turned out that the user permission READ_EXTERNAL_STORAGE was not added to the project list

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jxust.day07_08_contentprovider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

As a result, the relevant prompt information of the pictures in the SD card is displayed normally in Logcat.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659372&siteId=291194637