发布于 2016-05-03 11:03:52 | 131 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了android从系统图库中取图片的方法,涉及Android读取及选择图片等相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android从系统图库中取图片的实现方法。分享给大家供大家参考。具体如下:

在自己应用中,从系统图库中取图片,然后截取其中一部分,再返回到自己应用中。这是很多有关图片的应用需要的功能。

写了一个示例,上来就是个大按钮,连布局都不要了。最终,用选取图片中的一部分作为按钮的背景。
这里需要注意几点:

① 从图库中选取出来保存的图片剪辑,需要保存在sd卡目录,不能保存在应用自己的在内存的目录,因为是系统图库来保存这个文件,它没有访问你应用的权限;
② intent.putExtra("crop", "true")才能出剪辑的小方框,不然没有剪辑功能,只能选取图片;
③ intent.putExtra("aspectX", 1),是剪辑方框的比例,可用于强制图片的长宽比。

效果图如下:

Java代码如下:


package com.easymorse.gallery;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GalleryActivity extends Activity {
 private static int SELECT_PICTURE;
 private File tempFile;
 Button button;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.tempFile=new File("/sdcard/a.jpg");
  button = new Button(this);
  button.setText("获取图片");
  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    // intent.putExtra("aspectX", 1);
    // intent.putExtra("aspectY", 2);
    intent.putExtra("output", Uri.fromFile(tempFile));
    intent.putExtra("outputFormat", "JPEG");
    startActivityForResult(Intent.createChooser(intent, "选择图片"),
      SELECT_PICTURE);
   }
  });
  setContentView(button);
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
   if (requestCode == SELECT_PICTURE) {
    button.setBackgroundDrawable(Drawable.createFromPath(tempFile
      .getAbsolutePath()));
   }
  }
 }
}

希望本文所述对大家的Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务