Question

I have a pager activity in my android application I need to save the images according to there position in the pager. I managed to do the saving part but when iam in the first image i click save it saves the second image same for the second image it save the third i dont know whats wrong with my code! `

enter code here

public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    if (item.getItemId()==R.id.menuFinale) 
    {

        ImageView imageView = (ImageView) findViewById(R.id.image_one);
        imageView.setDrawingCacheEnabled(true);
        Bitmap bitmap = imageView.getDrawingCache();
        File root = Environment.getExternalStorageDirectory();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "My pic" ,"Saved to gallery");
        File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }



        return true;
    }
        else  {
                return super.onOptionsItemSelected(item);
            }
    }
Was it helpful?

Solution 2

I managed finally to solve my issue instead of the imageView I'm referring to cache I must instead refer to ViewPager to cache all including the imageView instead here is my new code

enter code here

public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    if (item.getItemId()==R.id.menuFinale) 
    {


        pager.setDrawingCacheEnabled(true);

        pager.buildDrawingCache(true);
        pager.setDrawingCacheEnabled(true);

        Bitmap b = pager.getDrawingCache(true);
        File root = Environment.getExternalStorageDirectory();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "My pic" ,"Saved to gallery");
        File file = new File(root.getAbsolutePath()+"/DCIM/HD.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            b.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return true;
    }
        else  
            {
                return super.onOptionsItemSelected(item);
            }     

}

OTHER TIPS

Try some thing as below :

 button=(Button)vi.findViewById(R.id.button_save);

        button.setOnClickListener(new OnClickListener() {

  private Bitmap bm;
  private String PREFS_NAME;
public void onClick(View arg0) {
    String root = Environment.getExternalStorageDirectory().toString();
       File myDir = new File(root + "/saved_images");
       if(!myDir.exists()){
           myDir.mkdirs();}
       bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
         holder.image.setImageBitmap(bm);
         SharedPreferences savedNumber = mContext.getSharedPreferences(PREFS_NAME, 0); 
        int lastSavedNumber = savedNumber.getInt("lastsavednumber",0); 
        lastSavedNumber++; 
        String fname = "Image-"+lastSavedNumber+".png"; 

        File file = new File (myDir, fname); 
        if (file.exists ()) {file.delete (); } 
        try { 
        FileOutputStream out = new FileOutputStream(file); 
        bm.compress(Bitmap.CompressFormat.PNG, 100, out);//Your Bitmap from the resouce 
        out.flush(); 
        out.close(); }
         catch (Exception e) { 
        e.printStackTrace(); 
        } 

        SharedPreferences saveNumber = mContext.getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 
        SharedPreferences.Editor editorset = saveNumber.edit(); 
        editorset.putInt("lastsavednumber",lastSavedNumber); 
        editorset.commit();
          Toast.makeText(mContext, "saved", Toast.LENGTH_SHORT). show();}});

hope help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top