package com.example.lianxi.fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.CursorLoader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.lianxi.R;
import com.example.lianxi.activity.MainActivity;
import com.example.lianxi.adapter.LeftAdapter;
import com.example.lianxi.util.BitmapUtil;
public class Fragment_Left extends Fragment {
private ListView listView;
private LeftAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.left,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.listview);
adapter = new LeftAdapter(getContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,100);
}
((MainActivity)getActivity()).showPage(position-1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==100&&resultCode==getActivity().RESULT_OK)
{
Uri uri = data.getData();
ProgressData(UrlToPath(uri));
}
super.onActivityResult(requestCode, resultCode, data);
}
private String UrlToPath(Uri uri){
String[] strings = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(getActivity(), uri, strings, null, null, null);
Cursor cursor = loader.loadInBackground();
int columnIndexOrThrow = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
return cursor.getString(columnIndexOrThrow);
}
private void ProgressData(String imageParh){
int size = getResources().getDimensionPixelSize(R.dimen.leftIconSize);
Bitmap bitmap = BitmapUtil.SclaeBitmap(imageParh, size, size);
adapter.setBitmap(bitmap);
}
}
工具类
package com.example.lianxi.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class BitmapUtil {
public static Bitmap SclaeBitmap(String imagePath,int width,int height){
//第一次加载图片的宽高信息
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath,options);
//根据图片设置原始高度 和请求宽高 计算采样率
options.inSampleSize = Math.max(options.outWidth/width,options.outHeight/height);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(imagePath,options);
}
}