1

我正在编辑一个开源应用程序:一个简单的儿童着色页应用程序。我需要能够让用户导入他自己的图像进行着色。这是完整的源代码。

这是从以下位置加载图像的代码R.drawable

public class StartNewActivity extends NoTitleActivity implements View.OnClickListener
{
    // This is an expensive operation.

    public static int randomOutlineId()
    {
        return new ResourceLoader().randomOutlineId();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Apparently this cannot be set from the style.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        setContentView(R.layout.start_new);

        GridView gridview = (GridView) findViewById(R.id.start_new_grid);
        gridview.setAdapter(new ImageAdapter(this));
    }

    public void onClick(View view)
    {
        setResult(view.getId());
        finish();
    }

    private static class ResourceLoader
    {

        ResourceLoader()
        {
            // Use reflection to list resource ids of thumbnails and outline
            // images.First, we list all the drawables starting with the proper
            // prefixes into 2 maps.
            Map<String, Integer> outlineMap = new TreeMap<String, Integer>();
            Map<String, Integer> thumbMap = new TreeMap<String, Integer>();
            Field[] drawables = R.drawable.class.getDeclaredFields();
            for (int i = 0; i < drawables.length; i++)
            {
                String name = drawables[i].getName();
                try
                {
                    if (name.startsWith(PREFIX_OUTLINE))
                    {
                        outlineMap.put(name.substring(PREFIX_OUTLINE.length()),
                                drawables[i].getInt(null));
                    }
                    if (name.startsWith(PREFIX_THUMB))
                    {
                        thumbMap.put(name.substring(PREFIX_THUMB.length()),
                                drawables[i].getInt(null));
                    }
                }
                catch (IllegalAccessException e)
                {
                }
            }
            Set<String> keys = outlineMap.keySet();
            keys.retainAll(thumbMap.keySet());
            _outlineIds = new Integer[keys.size()];
            _thumbIds = new Integer[keys.size()];
            int j = 0;
            Iterator<String> i = keys.iterator();
            while (i.hasNext())
            {
                String key = i.next();
                _outlineIds[j] = outlineMap.get(key);
                _thumbIds[j] = thumbMap.get(key);
                j++;
            }
        }

        public Integer[] getThumbIds()
        {
            return _thumbIds;
        }

        public Integer[] getOutlineIds()
        {
            return _outlineIds;
        }

        public int randomOutlineId()
        {
            return _outlineIds[new Random().nextInt(_outlineIds.length)];
        }
        private static final String PREFIX_OUTLINE = "outline";
        private static final String PREFIX_THUMB = "thumb";
        private Integer[] _thumbIds;
        private Integer[] _outlineIds;
    }

    private class ImageAdapter extends BaseAdapter
    {

        ImageAdapter(Context c)
        {
            _context = c;
            _resourceLoader = new ResourceLoader();
        }

        public int getCount()
        {
            return _resourceLoader.getThumbIds().length;
        }

        public Object getItem(int i)
        {
            return null;
        }

        public long getItemId(int i)
        {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            ImageView imageView;
            if (convertView == null)
            {
                // If it's not recycled, initialize some attributes
                imageView = new ImageView(_context);
                imageView.setLayoutParams(new GridView.LayoutParams(145, 145));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
                imageView.setOnClickListener(StartNewActivity.this);
            }
            else
            {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(_resourceLoader.getThumbIds()[position]);
            imageView.setId(_resourceLoader.getOutlineIds()[position]);
            return imageView;
        }
        private Context _context;
        private ResourceLoader _resourceLoader;
    }
}
4

1 回答 1

0

您可以使用File写入用户的导入文件。使用这样的东西。

public boolean write(byte[] data, File file)
{
    if (file.getParentFile().exists()) {
        if (file.exists()) {
            file.delete();
        }
    } else {
        file.getParentFile().mkdirs();
    }

    try{
        OutputStream output = new FileOutputStream(file);
        output.write(data);
        output.close();
        return true;
    }catch(Exception e){
        Log.v("FileManager", "Error writing file.", e);
        return false;
    }
}

样本:

String pathName = "/mnt/sdcard/Android/data/com.company.project/files/tmp/photo.png";
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

write(byteArray, new File("/mnt/sdcard/Android/data/com.company.project/files/photo.png");
于 2013-05-24T02:43:41.650 回答