如何加载原始图片

时间:2015-07-21 14:56:37

标签: android eclipse

虽然我需要加载原始图片,但只有一条错误消息:

  

位图太大而无法上传到纹理中(5312x2988,max = 4096x4096)

有谁知道如何解决这个问题?

public class MainActivity extends ListActivity {

    //define source of MediaStore.Images.Media, internal or external storage

    final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
    final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI; 

    final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
    final String source_DATA = MediaStore.Images.Media.DATA;

    final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;
    final String sourceId = MediaStore.Images.Media._ID;
    //SimpleCursorAdapter mySimpleCursorAdapter;
    MyAdapter mySimpleCursorAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        String[] from = {MediaStore.MediaColumns.TITLE};
        int[] to = {android.R.id.text1};

        CursorLoader cursorLoader = new CursorLoader(
            this, 
            sourceUri, 
            null, 
            null, 
            null, 
            MediaStore.Audio.Media.TITLE);

        Cursor cursor = cursorLoader.loadInBackground();

        mySimpleCursorAdapter = new MyAdapter(
            this, 
            android.R.layout.simple_list_item_1, 
            cursor, 
            from, 
            to, 
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        setListAdapter(mySimpleCursorAdapter);

        getListView().setOnItemClickListener(myOnItemClickListener);
    }

    OnItemClickListener myOnItemClickListener
        = new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
            Cursor cursor = mySimpleCursorAdapter.getCursor();
            cursor.moveToPosition(position);

            int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
            getThumbnail(int_ID);
        }};

    private Bitmap getThumbnail(int id){

        String[] thumbColumns = {source_DATA, sourceId};

        CursorLoader thumbCursorLoader = new CursorLoader(
            this, 
            sourceUri, 
            thumbColumns, 
            sourceId + "=" + id, 
            null, 
            null);

        Cursor thumbCursor = thumbCursorLoader.loadInBackground();

        Bitmap thumbBitmap = null;
        if(thumbCursor.moveToFirst()){
            int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);

            String thumbPath = thumbCursor.getString(thCulumnIndex);

            Toast.makeText(getApplicationContext(), 
                thumbPath, 
                Toast.LENGTH_LONG).show();

            thumbBitmap = BitmapFactory.decodeFile(thumbPath);

            //Create a Dialog to display the thumbnail
            AlertDialog.Builder thumbDialog = new AlertDialog.Builder(MainActivity.this);

            ImageView thumbView = new ImageView(MainActivity.this);

            thumbView.setImageBitmap(thumbBitmap);
            LinearLayout layout = new LinearLayout(MainActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(thumbView);
            thumbDialog.setView(layout);
            thumbDialog.show();
        } else {
            Toast.makeText(getApplicationContext(), 
                "NO Thumbnail!", 
                Toast.LENGTH_LONG).show();
        }

        return thumbBitmap;
    }

    public class MyAdapter extends SimpleCursorAdapter{

        Cursor myCursor;
        Context myContext;

        public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
            super(context, layout, c, from, to, flags);

            myCursor = c;
            myContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if(row==null){
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.row, parent, false);  
            }

            ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
            TextView textV = (TextView)row.findViewById(R.id.text);

            myCursor.moveToPosition(position);

            int myID = myCursor.getInt(myCursor.getColumnIndex(MediaStore.Images.Media._ID));
            String myData = myCursor.getString(myCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            textV.setText(myData);

            String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID, };
            CursorLoader thumbCursorLoader = new CursorLoader(
                myContext, 
                thumbUri, 
                thumbColumns, 
                thumb_IMAGE_ID + "=" + myID, 
                null, 
                null);
            Cursor thumbCursor = thumbCursorLoader.loadInBackground();

            Bitmap myBitmap = null;
            if(thumbCursor.moveToFirst()){
                int thCulumnIndex = thumbCursor.getColumnIndex(source_DATA);
                String thumbPath = thumbCursor.getString(thCulumnIndex);
                myBitmap = BitmapFactory.decodeFile(thumbPath);
                thumbV.setImageBitmap(myBitmap);
            }

            return row;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你可以看看Picasso。这是一个非常有用的加载图像的库

示例:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
 // more code

  [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
  return YES;
}

答案 1 :(得分:0)

您应该将其缩放到最大尺寸或更低。

您可以使用此代码获得最大尺寸

public ItemStack(Item item) : this(item.id, 1, 0) { } public ItemStack(Item item, int value) : this(item.id, value, 0) { } public ItemStack(Item item, int value, int value2) : this(item.id, value, value2) { }

使用

缩放位图
int[] maxSize = new int[1];
 gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);