IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Android 学习笔记-2011年7月

    lincyang发表于 2011-07-25 14:04:20
    love 0
    7月25日: 

    下面是从网上看到的,能解析音频文件的专辑信息的,比如专辑图片。

     

    public class Test extends Activity {
        /** Called when the activity is first created. */
        private ListView list;
        private static final String sExternalMediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString();
        private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Bitmap bm = null;
            long songid = -1,albumid = -1;
            Cursor cur = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,null, null, null);
            if(cur.moveToNext()){
                songid = cur.getLong(0);
                albumid = cur.getLong(12);
            }
            if (albumid < 0 && songid < 0) {
                throw new IllegalArgumentException("Must specify an album or a song id");
            }
    
            try {
                if (albumid < 0) {
                    Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
                    ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
                    if (pfd != null) {
                        FileDescriptor fd = pfd.getFileDescriptor();
                        bm = BitmapFactory.decodeFileDescriptor(fd);
                    }
                } else {
                    Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
                    ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
                    if (pfd != null) {
                        FileDescriptor fd = pfd.getFileDescriptor();
                        bm = BitmapFactory.decodeFileDescriptor(fd);
                    }
                }
            } catch (FileNotFoundException ex) {
                //
            }
            BitmapDrawable bmpDraw = new BitmapDrawable(bm);
            ImageView image = (ImageView)findViewById(R.id.album);
            image.setImageDrawable(bmpDraw);
        }
       
    }

    获取图片:
    private String getAlbumArt(int trackId) {//trackId是音乐的id
            String mUriTrack = "content://media/external/audio/media/#";
            String[] projection = new String[] {"album_id"};
            String selection = "_id = ?";
            String[] selectionArgs = new String[] {Integer.toString(trackId)};        
            Cursor cur = context.getContentResolver().query(Uri.parse(mUriTrack), projection, selection, selectionArgs, null);
            int album_id = 0;
            if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
                cur.moveToNext();
                album_id = cur.getInt(0);
            }
            cur.close();
            cur = null;
    
            if (album_id < 0) {
                return null;
            }
            String mUriAlbums = "content://media/external/audio/albums";
            projection = new String[] {"album_art"};
            cur = context.getContentResolver().query(Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)), projection, null, null, null);
    
            String album_art = null;
            if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
                cur.moveToNext();
                album_art = cur.getString(0);
            }
            cur.close();
            cur = null;
                
            return album_art;
        }


    7月31日 图片缩放

    这是来自eoe的代码,我找来尝试解析时间:

    是专门实现图片放大变小的代码,
    package com.jinyan.TestImage;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class TestImage extends Activity {
            /** Called when the activity is first created. */
    
            private static final String tag = "TestImage.............";
            
            String imageUrl = "http://i.pbase.com/o6/92/229792/1/80199697.uAs58yHk.50pxCross_of_the_Knights_Templar_svg.png";
            Bitmap bmImg;
            ImageView imView;
    
            Button button1;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    imView = (ImageView) findViewById(R.id.imview);
                    imView.setImageBitmap(returnBitMap(imageUrl));
    
                    Bitmap bitmap = returnBitMap(imageUrl);
    
                    imView.setImageDrawable(resizeImage(bitmap, 600, 600));
            }
            
            public Bitmap returnBitMap(String url) {
                    URL myFileUrl = null;
                    Bitmap bitmap = null;
                    try {
                            myFileUrl = new URL(url);
                    } catch (MalformedURLException e) {
                            e.printStackTrace();
                    }
                    try {
                            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                                            .openConnection();
                            conn.setDoInput(true);
                            conn.connect();
                            InputStream is = conn.getInputStream();
                            bitmap = BitmapFactory.decodeStream(is);
                            is.close();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
                    Log.v(tag,bitmap.toString());
                    
                    return bitmap;
            }
    
            public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
    
                    // load the origial Bitmap
                    Bitmap BitmapOrg = bitmap;
    
                    int width = BitmapOrg.getWidth();
                    int height = BitmapOrg.getHeight();
                    int newWidth = w;
                    int newHeight = h;
                    
                    Log.v(tag,String.valueOf(width));
                    Log.v(tag,String.valueOf(height));
                    
                    Log.v(tag,String.valueOf(newWidth));
                    Log.v(tag,String.valueOf(newHeight));
    
                    // calculate the scale
                    float scaleWidth = ((float) newWidth) / width;
                    float scaleHeight = ((float) newHeight) / height;
    
                    // create a matrix for the manipulation
                    Matrix matrix = new Matrix();
                    // resize the Bitmap
                    matrix.postScale(scaleWidth, scaleHeight);
                    // if you want to rotate the Bitmap
                    // matrix.postRotate(45);
    
                    // recreate the new Bitmap
                    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                                    height, matrix, true);
    
                    // make a Drawable from Bitmap to allow to set the Bitmap
                    // to the ImageView, ImageButton or what ever
                    return new BitmapDrawable(resizedBitmap);
    
            }
            
    }




沪ICP备19023445号-2号
友情链接