安卓使用ContentResolver将图片保存到手机的多媒体数据中

使用ContentResolver将图片保存到手机的多媒体数据中

效果:

安卓使用ContentResolver将图片保存到手机的多媒体数据中_第1张图片

核心代码

package com.javen.devicemange.CrazyOne.content;

import android.Manifest;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.javen.devicemange.R;

import java.io.OutputStream;

/**
 * Created by Administrator on 2017/2/16 0016.
 * 使用将图片ContentResolver保存到手机的多媒体数据中
 * 
 * 
 */

public class AddPhotoToAlbum extends AppCompatActivity implements View.OnClickListener {
    private ImageView imageView10;
    private Button add;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addphototoalbum);
        initView();
        Log.d("GsonUtils", "onCreate=");
        //需要申请读取和修改外置存储的权限
        requestPermission();
    }

    private void initView() {
        imageView10 = (ImageView) findViewById(R.id.imageView10);
        add = (Button) findViewById(R.id.add);

        add.setOnClickListener(this);
    }

    private void requestPermission() {
        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
        };
        ActivityCompat.requestPermissions(this, permissions, 1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                AddPictureToAlbum();
                break;
        }
    }

    private void AddPictureToAlbum() {
        Log.d("GsonUtils", "AddPictureToAlbum=");
        //创建ContentValues对象,准备插入数据
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "shuangta");
        contentValues.put(MediaStore.Images.Media.DESCRIPTION, "双塔");
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        //插入数据,返回所插入数据对应的Uri
        Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        //加载应用程序res下的图片bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shuangta);
        OutputStream outputStream = null;
        try {
            //获取刚插入的数据的Uri对应的输出流
            outputStream = getContentResolver().openOutputStream(uri);
            //bitmap图片保存到Uri对应的数据节点中
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            //图片会保存到sd卡的pcitures目录下1487231905572.jpg
            outputStream.close();
            Log.d("GsonUtils", "outputStream=");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("GsonUtils", "Exception=" + e.toString());
        }
    }
}

布局xml

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

            android:id="@+id/imageView10"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@drawable/shuangta"/>
    


。。。

你可能感兴趣的:(安卓图片)