我瀏覽過與SO相似的多個主題,但找不到解決方案。我有一個ViewPager的活動。它使用Picasso在ImageView中加載圖像。它工作正常,併成功加載圖像,我可以通過它們滑動。但問題是我想保存當前的觀看圖像或可繪製的。我可以編碼保存,但爲此,我首先必須得到drawable,而我似乎無法做到這一點。如何從ViewPager獲取當前查看圖像?
XML:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent" />
SwipeActivity.java
public class SwipeActivity extends AppCompatActivity {
private ViewPager viewPager;
private ImageView imageView;
public void save(){
int currentItem =viewPager.getCurrentItem();
Drawable drawable = (Drawable) imageView.getDrawable().getCurrent();
ImageView am = (ImageView)imageView.getTag(currentItem);
Log.e("Jatt", "save: "+currentItem);
//Lots of Confustion here. Don't look at this function
//I have messed it up. But will correct once I get the way
Bitmap bm = ((BitmapDrawable)am.getDrawable()).getBitmap();
//Bitmap bm =((BitmapDrawable) drawable).getBitmap();
storeImage(bm);
}
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
private void storeImage(Bitmap image) {
String TAG="Jatt";
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.saveimage, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
save();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(" ");
viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(getIntent().getIntExtra("pos",0));
}
class ImagePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return Photos.sendToList.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = SwipeActivity.this;
imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//imageView.setImageResource(Photos.sendToList[position]);
Picasso.with(context).load(Photos.sendToList[position]).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
我一直在使用這種嘗試:
imageView.getDrawable(); //Return a fixed image no matter which image is
// being currently viewed
這也太:
imageView.getDrawable().getCurrent(); //Doesn't return currently viewing img
我也曾嘗試getTag和setTag種方式,但我得到的NullPointerException每次我雖然正確設置標籤位置
不能相信不能在24小時內一個單一的答案/評論爲一個簡單的問題:( –