Android上传连接手机上的图片

来源:互联网 发布:成都理工大学网络 编辑:程序博客网 时间:2024/04/27 15:42

1. ImageView图片框资源来自内存(内存放在项目的Drawable

这种很简单,就直接把图片复制到Drawable下面,然后za再调用就好了,

这个方法是我们前台的一个实现后台代码的一个class,这个方法是在继承 AppCompatActivity 后重写的一个方法 也就是当一加载界面的时候,这个方法就会运行:

<ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/iv_second_image1"    android:src="@drawable/s1"    />
private int images []={R.drawable.shsf,R.drawable.s26,R.drawable.s25,R.drawable.s15,R.drawable.s26,R.drawable.dsf};
private int currentIndex=0;


@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_second);    //找到你要放入图片的ImageView:   ImageView iv_second_image1= (ImageView) findViewById(R.id.iv_second_image1);  
   iv_second_image1.setImageResource(images[currentIndex]);
这样就行了,

2.图片资源来源内存卡,因为我是连接手机的,所以我直接用手机上面图片上传,下面来分享一下:

访问手机中的图片之前 必须要弄一下权限,在manifests中设置:

<!--读取内存卡权限--><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
就好了,不然访问不了。你得到会是空值:

这个方法是我们前台的一个实现后台代码的一个class,这个方法是在继承 AppCompatActivity 后重写的一个方法 也就是当一加载界面的时候,这个方法就会运行:
private int currentIndex=0;private int currentAplha=255;
private File [] files;
private Bitmap bitmap;

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_second);    //找到你要放入图片的ImageView:    iv_second_image1= (ImageView) findViewById(R.id.iv_second_image1);    //判断内存卡是否可用:    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){        //获取内存卡路径:        String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();        //因为我图片存在我的 G160828文件夹下面 所以:        File file=new File(sdCardPath+"/G160828/");        //获得文件夹下面的所以文件:      files=file.listFiles();             }  //然后再给ImageView设置图片:    bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());    iv_second_image1.setImageBitmap(bm);
3.再和大家说一下 关于图片上一张下一张的与透明度的代码与思路:再第二题的代码下衍生:
3.1点击上一张图片 然后执行的代码:
public void pre(View view){    currentIndex--;    if(currentIndex<0){        currentIndex=0;        Toast.makeText(this,"这已经是第一张了",Toast.LENGTH_LONG).show();    }    //放入图片:    bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());    
    iv_second_image1.setImageBitmap(bitmap);

}
3.2点击下一张图片 然后执行的代码:
public void next(View view){    currentIndex++;    if(currentIndex>=files.length-1){        currentIndex=files.length-1;        Toast.makeText(this, "这 已经是最后一张了", Toast.LENGTH_SHORT).show();    }    bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());    iv_second_image1.setImageBitmap(bitmap);}
4关于增加透明度于降低透明度:透明度最高是255,也就是原图 (Aplha)
//设置透明度:public void add(View view){    currentAplha+=20;    if(currentAplha>=255){        currentAplha=255;    }  iv_second_image1.setImageAlpha(currentAplha);}public void sub(View view){    currentAplha-=20;    if(currentAplha<=0){        currentAplha=0;    }    iv_second_image1.setImageAlpha(currentAplha);}
5.再和大家说一下 关于你触摸一张图片它就会显示它的局部给你看的那种,类似放大镜那种:
第一步,需要在前台设置一张 ImageView,它的长宽代表你所看到方法后局部的长宽:
<ImageView    android:layout_width="300dp"    android:layout_height="300dp"    android:background="#fdf9f9"    android:id="@+id/iv_second_image2"    />
然后在这个方法里写:
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);
//给ImageView设置触摸事件:iv_second_image1.setOnTouchListener(new View.OnTouchListener() {    @Override    public boolean onTouch(View v, MotionEvent event) {        float x=event.getX();        float y=event.getY();           //抠图:        Bitmap bitmap2=Bitmap.createBitmap(bitmap,(int)x,(int)y,50,50);  
bitmap代表Bitmap bitmap=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath()); 即是就是路劲,就是你触摸那种图片的路径        iv_second_image2.setImageBitmap(bitmap2);        return true;    }});
}
如果有什么交流 可以给我

阅读全文
0 0
原创粉丝点击