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"-->
    <!--/>-->

猜你喜欢

转载自blog.csdn.net/f_1314520/article/details/82770199