问题:从查看传呼机保存图像 [英] Issue : Saving Image from View Pager
本文介绍了问题:从查看传呼机保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我加载从服务器映像集和ViewPager显示。当我打保存按钮,接下来的图像保存到SD卡,而不是精确的图像。例如,如果我点击图片第3,第4影像被保存,如果我点击图片4,第5图像保存到SD卡。如何处理这个?
I'm loading set of images from server and displaying in ViewPager. When I hit Save button, the next image is saved to SD Card and not the exact image. For example, If i click 3rd image, 4th image is saved and if i click 4th image, 5th image is saved to SD card. How to handle this??
public class ImagePagerActivity extends ActionBarActivity {
ImageView imageView;
BitmapDrawable btmpDr;
private static final String STATE_POSITION = "STATE_POSITION";
@InjectView(R.id.pager)
ViewPager pager;
ActionBar actionBar;
PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_pager);
ButterKnife.inject(this);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
int pagerPosition = bundle.getInt("position", 0);
String[] imageUrls = bundle.getStringArray("images");
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
pagerAdapter = new ImagePagerAdapter(imageUrls);
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}
public class ImagePagerAdapter extends PagerAdapter {
public String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image,
view, false);
assert imageLayout != null;
imageView = (ImageView) imageLayout
.findViewById(R.id.image);
Picasso.with(ImagePagerActivity.this).load(images[position]).into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_image_pager, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
Bitmap bitmap;
OutputStream output;
BitmapDrawable btmpDr = (BitmapDrawable) imageView.getDrawable();
bitmap = btmpDr.getBitmap();
File filepath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File dir = new File(filepath.getAbsolutePath()
+ "/Test Folder/");
dir.mkdirs();
Date d = new Date();
CharSequence s = DateFormat
.format("dd-MM-yy hh-mm-ss", d.getTime());
File file = new File(dir, s + ".png");
Message.Toast(ImagePagerActivity.this, "Image Saved to SD Card");
try {
output = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
break;
case android.R.id.home:
NavUtils.navigateUpTo(this, new Intent(this, GalleryActivity.class));
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
尝试添加 file.createNewFile()
:
try {
file.createNewFile(); //add here
output = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
这篇关于问题:从查看传呼机保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文