通用图像加载器 - 将图像保存到SD卡

时间:2015-01-21 02:04:43

标签: java android bitmap android-viewpager universal-image-loader

我正在为我的应用使用通用图片加载器。我想将ViewPager显示的当前图像保存到SD卡,但是我下面的代码将错误的图像保存到SD卡。它只是随机保存图像。我需要知道如何检索图像的当前位图并保存它。我不知道获取当前图像的位图并将其保存到SD卡的任何其他替代方法。我目前获取位图的方式是通过" Bitmap bitmap = loadedImage"。非常感谢您的帮助。 Cheeers。

 public class ImagePagerFragment extends BaseFragment implements View.OnClickListener {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_pager, container, false);
ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
pager.setAdapter(new ImageAdapter());
pager.setCurrentItem(getArguments().getInt(Constants.Extra.IMAGE_POSITION,  0));

return rootView;
} 

private class ImageAdapter extends PagerAdapter {

@Override
public Object instantiateItem(ViewGroup view, int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);

assert imageLayout != null;
final christ.triumphant.TouchImageView imageView = (christ.triumphant.TouchImageView) imageLayout.findViewById(R.id.imagei);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

ImageLoader.getInstance().displayImage(imageUrls[position], imageView, options, new SimpleImageLoadingListener() {

..
@Override
public void onLoadingComplete(String imageUri, View view, final Bitmap        loadedImage) {
        spinner.setVisibility(View.GONE);

        isave = (Button) getView().findViewById(R.id.save);
        isave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {

                Bitmap bitmap = loadedImage;

                File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/HD GOSPEL LOCKSCREENS");

                File f = new File(folder, String.valueOf(System.currentTimeMillis()) + "HDGL.PNG");

                try {

                    FileOutputStream out = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                    Toast.makeText(getActivity(), "Image Successfully Saved", Toast.LENGTH_LONG).show();

                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
                                {
                                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                    File ff = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
                                    Uri contentUri = Uri.fromFile(ff);
                                    mediaScanIntent.setData(contentUri);
                                    getActivity().sendBroadcast(mediaScanIntent);
                                }
                                else
                                {
                                    getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
                                }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });


        ishare = (Button) getView().findViewById(R.id.share);
        ishare.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View view) {

                Bitmap icon = loadedImage;
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
                try {
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
                startActivity(Intent.createChooser(share, "Share Image"));

                Toast.makeText(getActivity(), "Image Successfully Shared", Toast.LENGTH_LONG).show();
            }

        });

    }
});

view.addView(imageLayout, 0);
return imageLayout;
}

... ... ... }

0 个答案:

没有答案