Java e、 设置图像位图(缩略图); root.addView(ivImage); //setContentView(根目录); //ivImage.setImageBitmap(缩略图); } @抑制警告(“弃用”) 从GalleryResult(意向数据)中

Java e、 设置图像位图(缩略图); root.addView(ivImage); //setContentView(根目录); //ivImage.setImageBitmap(缩略图); } @抑制警告(“弃用”) 从GalleryResult(意向数据)中,java,android,android-studio,converter,gif,Java,Android,Android Studio,Converter,Gif,e、 设置图像位图(缩略图); root.addView(ivImage); //setContentView(根目录); //ivImage.setImageBitmap(缩略图); } @抑制警告(“弃用”) 从GalleryResult(意向数据)中选择的专用void{ Uri selectedImageUri=data.getData(); 字符串[]投影={MediaStore.MediaColumns.DATA}; Cursor Cursor=managedQuery(选择图像URI

e、 设置图像位图(缩略图); root.addView(ivImage); //setContentView(根目录); //ivImage.setImageBitmap(缩略图); } @抑制警告(“弃用”) 从GalleryResult(意向数据)中选择的专用void{ Uri selectedImageUri=data.getData(); 字符串[]投影={MediaStore.MediaColumns.DATA}; Cursor Cursor=managedQuery(选择图像URI、投影、null、null、, 无效); int column_index=cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); String selectedImagePath=cursor.getString(列索引); 位图bm; BitmapFactory.Options=new-BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeFile(选择图像路径,选项); 所需的最终int_尺寸=200; int标度=1; 而(options.outWidth/scale/2>=所需尺寸 &&options.outHeight/scale/2>=所需尺寸) 比例*=2; options.inSampleSize=比例; options.inJustDecodeBounds=false; bm=BitmapFactory.decodeFile(选择图像路径,选项); 最终图像视图ivImage=新图像视图(本); ivImage.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ 如果(zoomOut){ Toast.makeText(getApplicationContext(),“正常大小!”,Toast.LENGTH\u LONG.show(); ivImage.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容)); ivImage.setAdjustViewBounds(真); zoomOut=false; }否则{ Toast.makeText(getApplicationContext(),“全屏!”,Toast.LENGTH\u LONG.show(); ivImage.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.MATCH_父项)); iImage.setScaleType(ImageView.ScaleType.FIT_XY); zoomOut=true; } } }); Display Display=getWindowManager().getDefaultDisplay(); int width=display.getWidth(); int height=display.getHeight(); i图像设置最小宽度(宽度); i图像设置最小高度(高度); ivImage.setMaxWidth(宽度); ivImage.setMaxHeight(高度); ivImage.setLayoutParams(新的ActionBar.LayoutParams( 1000, 1000)); ivImage.setImageBitmap(bm); root.addView(ivImage); setContentView(根目录); ivImage.setImageBitmap(bm); } 专用字节[]generateGIF(){ ArrayList位图=新的ArrayList(); 观点五; ImageView iv; for(int i=0;i使用此库;它非常适合多图像选择;你能帮我配置这个库或添加它吗?我不知道@试着完成这项活动;我应该把它粘贴到我的主要活动中吗?在哪里?android初学者??
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/* video/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
public class MainActivity extends AppCompatActivity {

/**
 * this is the destination of the new GIF file, it will be saved directly in the SD card
 * (internal storage) in a file named "test.gif"
 */
private static final String IMAGE_PATH = "/sdcard/test.gif";
private static final int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private boolean zoomOut = false;
private Button btnSelect;
private LinearLayout root;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
    root = (LinearLayout) findViewById(R.id.ll);

    btnSelect.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });

    ImageView ivImage = (ImageView) findViewById(R.id.ivImage);
    ivImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (root.getChildCount() == 0) {
                return;
            }

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(IMAGE_PATH);
                outStream.write(generateGIF());
                outStream.close();
                Toast.makeText(MainActivity.this,
                        "GIF saved to " + IMAGE_PATH, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (outStream != null) {
                    try {
                        outStream.flush();
                        outStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
            }
        }
    });
}

private void selectImage() {
    final CharSequence[] items = {"Take Photo", "Choose from Library",
            "Cancel"};

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Library")) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/* video/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ClipData clipData = data.getClipData();
    for (int i = 0; i < clipData.getItemCount(); i++)
    {
        Uri uri = clipData.getItemAt(i).getUri();
    }

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    Bitmap resized = Bitmap.createScaledBitmap(thumbnail, 800, 150, true);
    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ImageView ivImage = new ImageView(this);
    GradientDrawable gd = new GradientDrawable();
    gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
    gd.setCornerRadius(5);
    gd.setStroke(1, 0xFF000000);

    ivImage.setBackground(gd);


// the following code causes a crash "NullPointerException" :

//        Point point = null; // --> the reason for the crash
//        getWindowManager().getDefaultDisplay().getSize(point);
//        int width = point.x;
//        int height = point.y;
//
//        ivImage.setMinimumWidth(width);
//        ivImage.setMinimumHeight(height);
//
//        ivImage.setMaxWidth(width);
//        ivImage.setMaxHeight(height);
//        ivImage.getLayoutParams().width = 20; // --> another crash happens here
//        ivImage.getLayoutParams().height = 20;


    ivImage.setLayoutParams(new ActionBar.LayoutParams(
            GridLayout.LayoutParams.WRAP_CONTENT,
            GridLayout.LayoutParams.MATCH_PARENT));
    ivImage.setImageBitmap(thumbnail);
    root.addView(ivImage);

//        setContentView(root);
//        ivImage.setImageBitmap(thumbnail);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    Uri selectedImageUri = data.getData();
    String[] projection = {MediaStore.MediaColumns.DATA};
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
            null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();

    String selectedImagePath = cursor.getString(column_index);

    Bitmap bm;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath, options);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(selectedImagePath, options);
    final ImageView ivImage = new ImageView(this);
    ivImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (zoomOut) {
                Toast.makeText(getApplicationContext(), "NORMAL SIZE!", Toast.LENGTH_LONG).show();
                ivImage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ivImage.setAdjustViewBounds(true);
                zoomOut = false;
            } else {
                Toast.makeText(getApplicationContext(), "FULLSCREEN!", Toast.LENGTH_LONG).show();
                ivImage.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                ivImage.setScaleType(ImageView.ScaleType.FIT_XY);
                zoomOut = true;
            }
        }
    });
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    ivImage.setMinimumWidth(width);
    ivImage.setMinimumHeight(height);

    ivImage.setMaxWidth(width);
    ivImage.setMaxHeight(height);
    ivImage.setLayoutParams(new ActionBar.LayoutParams(
            1000,
            1000));
    ivImage.setImageBitmap(bm);
    root.addView(ivImage);
    setContentView(root);
    ivImage.setImageBitmap(bm);

}

private byte[] generateGIF() {

    ArrayList<Bitmap> bitmaps = new ArrayList<>();

    View v;
    ImageView iv;
    for (int i = 0; i < root.getChildCount(); i++) {
        v = root.getChildAt(i);
        if (v instanceof ImageView) {
            iv = (ImageView) v;
            bitmaps.add(((BitmapDrawable) iv.getDrawable()).getBitmap());
        }
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    encoder.start(bos);
    for (Bitmap bitmap : bitmaps) {
        encoder.addFrame(bitmap);
    }
    encoder.finish();
    return bos.toByteArray();
}

}