Android 获取手机模拟器sd卡图片及截取图片

需把图片保存到找到手机模拟器(夜神模拟器)sd卡中的图片路径:文件管理器/mnt/sdcard/images(images是自己创的文件夹)

java代码:
package com.example.android_07;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

/**

  • Created by Administrator on 2018/1/23 0023.
    */

public class MainActivity extends AppCompatActivity {

private ImageView iv_main_image;
private File[] imageShows;

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

// 找到imageview控件
iv_main_image = findViewById(R.id.iv_main_image);
// 判断sd卡是否存在
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// 读取sd卡的绝对路径
String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();

// 获取sd卡中所有的文件,得到了一个图片数组
File file = new File(absolutePath + “/images”);
imageShows = file.listFiles();
}

    //   我们通过控制数组下标展示图片
    final Bitmap bitmap = BitmapFactory.decodeFile(imageShows[0].getAbsolutePath());
    iv_main_image.setImageBitmap(bitmap);

// 找到imageview控件
// 获取手点击的位置
iv_main_image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// 将横纵坐标扩张,然后再原图扣取一小块图片
// 将这一小块图片放大转成位图
float x = motionEvent.getX();
float y = motionEvent.getY();
Bitmap newBitmap = Bitmap.createBitmap(bitmap,(int)x,(int)y,50,50);
// 最终将位图放到指定的位置
ImageView iv_main_imageNew = findViewById(R.id.iv_main_imageNew);
iv_main_imageNew.setImageBitmap(newBitmap);
return true;
}
});
}
}
//xml

<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
<!--xmlns:tools="http://schemas.android.com/tools"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:orientation="vertical"-->
<!--tools:context=".MainActivity">-->
<!--<ImageView-->
    <!--android:layout_width="300dp"-->
    <!--android:layout_height="300dp"-->
    <!--android:id="@+id/iv_main_image"-->
    <!--/>-->

<!--<ImageView-->
    <!--android:layout_width="100dp"-->
    <!--android:id="@+id/iv_main_imageNew"-->
    <!--android:layout_height="100dp"-->
    <!--/>-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值