我有一个ImageView,我从服务器获取它的内容,有人将其发送到服务器。我想改变照片的大小以适合我想要的;为此,我成功了,我可以调整它,但问题是它降低了图像质量,因为我将该照片发送到另一个活动,因此我需要图像的原始质量。下面我将发布课程,希望你能帮助我:
public class welcome extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
make_category_list();
clearCache();
lv = (ListView) findViewById(R.id.ads_list);
make_all_ads_list();
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int i) {
if (i == 1) {
if (view.getId() == lv.getId()) {
int currentFirstVisibleItem = lv.getFirstVisiblePosition();
int mLastFirstVisibleItem = lv.getLastVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem)
{
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
// go down
if (goNext == true) {
goNext = false;
make_all_ads_list();
pDialog.dismiss();
}
}
}
}
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2)
{
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getApplicationContext(), show_full_ads.class);
intent.putExtra("ads", all_ads.get(position));
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
clearCache();
}
public void make_all_ads_list() {
try
{
DownloadTask dl = new DownloadTask();
dl.execute(url_ads + current_page);
current_page++;
}
catch (Exception e)
{
Log.i("NaqibMessage", "error in ads Class in method make_all_ads_list -> " + e.toString());
}
}
private class DownloadTask extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(welcome.this);
pDialog.setMessage("درحال آماده سازی");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
String temp = "";
try {
JSONDownloader jd = new JSONDownloader();
temp = jd.downloadURL(params[0]);
}
catch (Exception e)
{
Log.i("NaqibMessage", "error in ads Class in DownloadTask -> " + e.toString());
}
return (temp);
}
@Override
protected void onPostExecute(String s)
{
ListViewLoaderTask loader = new ListViewLoaderTask();
loader.execute(s);
pDialog.dismiss();
}
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter> {
@Override
protected SimpleAdapter doInBackground(String... params)
{
try
{
AdsParser parser = new AdsParser();
all_ads.addAll(parser.parse(params[0]));
}
catch (Exception e)
{
Log.i("NaqibMessage", "error in ads Class in ListViewLoaderTask-> " + e.toString());
}
String[] from = {"image", "title", "intro", "date"};
int[] to = {R.id.ads_img, R.id.ads_title, R.id.ads_intro, R.id.ads_date};
SimpleAdapter adb = new SimpleAdapter(getBaseContext(), all_ads, R.layout.ads_list_row, from, to);
return adb;
}
@Override
protected void onPostExecute(SimpleAdapter adapter) {
lv.setAdapter(adapter);
for (int i = 0; i < adapter.getCount(); i++) {
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
String imgURL = (String) hm.get("image_path");
HashMap<String, Object> forDownload = new HashMap<>();
forDownload.put("image_path", imgURL);
forDownload.put("position", i);
ImageDownloaderTask imageDownloader = new ImageDownloaderTask();
imageDownloader.execute(forDownload);
}
goNext = true;
}
}
private class ImageDownloaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>> {
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... params) {
InputStream myStream;
String imgURL = (String) params[0].get("image_path");
int position = (Integer) params[0].get("position");
try {
URL url = new URL(imgURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
myStream = connection.getInputStream();
File cacheDirectory = getBaseContext().getCacheDir();
File temp = new File(cacheDirectory.getPath() + "/image_" + position + "_" + current_page + ".png");
FileOutputStream outStream = new FileOutputStream(temp);
Bitmap b = BitmapFactory.decodeStream(myStream);
//Bitmap resizedBmp = Bitmap.createScaledBitmap(b, 140, 150, true);
b.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
HashMap<String, Object> bitmap = new HashMap<>();
bitmap.put("image", temp.getPath());
bitmap.put("position", position);
return (bitmap);
} catch (Exception e) {
/*
Log.i("NaqibMessage" , "error in ads Class in ImageDownloaderTask -> " + e.toString());
*/
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, Object> result) {
String image = (String) result.get("image");
int position = (Integer) result.get("position");
SimpleAdapter adb = (SimpleAdapter) lv.getAdapter();
HashMap<String, Object> hm = (HashMap<String, Object>) adb.getItem(position);
hm.put("image", image);
adb.notifyDataSetChanged();
}
}
public void clearCache() {
try {
File[] f = getBaseContext().getCacheDir().listFiles();
for (File file : f) {
file.delete();
}
} catch (Exception e) {
Log.i("NaqibMessage", "error in ads Class in ClearCache -> " + e.toString());
}
}
我删除了不必要的代码,你可以看到当我点击LIstView项目时我用它发送数据,在代码的最后部分你可以看到ImageDownloader类。
答案 0 :(得分:0)
我可以在保留原始图像的同时调整图像大小吗?
是:
步骤1:将原始图像下载到一个文件中。
步骤2:调整已下载的图像的大小,如果需要,将输出写入第二个文件。