The problem of save image file

Put your problem here if it does not fit any of the other categories.

The problem of save image file

Postby NeoLiu » Tue Jan 15, 2008 5:41 am

:(
Hi guys

i am affliction of the problem of save image file

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void OutPutPicture(Context context)
  2.  
  3.     {  
  4.  
  5.                 FileOutputStream fOut = null;
  6.  
  7.                 try {
  8.  
  9.                         fOut = context.openFileOutput("pic1.png", Context.MODE_PRIVATE);
  10.  
  11.                 } catch (FileNotFoundException e) {
  12.  
  13.                         e.printStackTrace();
  14.  
  15.                 }
  16.  
  17.                
  18.  
  19.                 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  20.  
  21.  
  22.  
  23.         try {
  24.  
  25.                 fOut.flush();
  26.  
  27.                 } catch (IOException e) {
  28.  
  29.                         e.printStackTrace();
  30.  
  31.                 }
  32.  
  33.         try {
  34.  
  35.                 fOut.close();
  36.  
  37.                 } catch (IOException e) {
  38.  
  39.                         e.printStackTrace();
  40.  
  41.                 }      
  42.  
  43.     }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



but catch FileNotFoundException, fOut is null.
i don't why the fOut is null :(
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Postby plusminus » Tue Jan 15, 2008 11:46 pm

Hello NeoLiu,

your code looks correct. Did you try to place the saving part right into the onCreate() of an empty Activity :?:
Where does your context-parameter come from (Hint: Activity extends Context!) :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: The problem of save image file

Postby NeoLiu » Wed Jan 16, 2008 4:27 am

Hi plusminus

I tested the code into a empty activity onCreate();

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.test;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6.  
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.os.Bundle;
  12.  
  13. public class Test extends Activity {
  14.     /** Called when the activity is first created. */
  15.         private static Bitmap BitmapA;
  16.     @Override
  17.     public void onCreate(Bundle icicle) {
  18.         super.onCreate(icicle);
  19.         setContentView(R.layout.main);
  20.        
  21.         BitmapA = Bitmap.createBitmap(320, 320, true);
  22.         OutPutPicture();
  23.     }
  24.    
  25.     public void OutPutPicture()
  26.     {  
  27.           FileOutputStream fOut = null;
  28.           try {
  29.                fOut = openFileOutput("pic1.png", MODE_PRIVATE);
  30.           } catch (FileNotFoundException e) {
  31.                e.printStackTrace();
  32.           }
  33.          
  34.           BitmapA.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  35.  
  36.         try {
  37.           fOut.flush();
  38.           } catch (IOException e) {
  39.                e.printStackTrace();
  40.           }
  41.         try {
  42.           fOut.close();
  43.           } catch (IOException e) {
  44.                e.printStackTrace();
  45.           }  
  46.     }
  47. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


that is OK,

but same code in my app fOut is null

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. OutPutPicture(Context context)
  2. {
  3. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

in my view.java
the context-parameter is come from Activity.java

now i modify the code put the OutPutPicture() into Activity.java
failed, the fOut is null
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Postby plusminus » Wed Jan 16, 2008 5:31 pm

Hello NeoLiu,

sounds strange, post your whole View-Class plus the line where you create it (like: new MyView(this, ...) ).
I'll have a look.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: The problem of save image file

Postby NeoLiu » Wed Jan 16, 2008 6:15 pm

ImagePS.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.imageps;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.app.AlertDialog;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.text.Layout;
  12.  
  13. import android.view.KeyEvent;
  14.  
  15. import android.view.Menu;
  16.  
  17. import android.view.View;
  18.  
  19. import android.view.Window;
  20.  
  21. import android.widget.AdapterView;
  22.  
  23. import android.widget.BaseAdapter;
  24.  
  25. import android.widget.Gallery;
  26.  
  27.  
  28.  
  29. import java.io.FileNotFoundException;
  30.  
  31. import java.io.FileOutputStream;
  32.  
  33. import java.io.IOException;
  34.  
  35. import java.util.ArrayList;
  36.  
  37.  
  38.  
  39. import android.content.*;
  40.  
  41. import android.content.DialogInterface.OnClickListener;
  42.  
  43. import android.view.*;
  44.  
  45. import android.widget.*;
  46.  
  47. import android.widget.Gallery.LayoutParams;
  48.  
  49. import android.graphics.Bitmap;
  50.  
  51. import android.graphics.drawable.Drawable;
  52.  
  53.  
  54.  
  55.  
  56.  
  57. //Need the following import to get access to the app resources, since this
  58.  
  59. //class is in a sub-package.
  60.  
  61.  
  62.  
  63.  
  64.  
  65. /**
  66.  
  67.  * Demonstrates the handling of touch screen events to implement a simple
  68.  
  69.  * painting app.
  70.  
  71.  */
  72.  
  73. public class ImagePS extends Activity{
  74.  
  75.         private static ImagePSView mImagePSView;
  76.  
  77.         private static Bundle extras;
  78.  
  79.         private static final int MENU_SAVE = Menu.FIRST;
  80.  
  81.        
  82.  
  83.     @Override
  84.  
  85.     protected void onCreate(Bundle icicle) {
  86.  
  87.         super.onCreate(icicle);
  88.  
  89.        
  90.  
  91.         extras = getIntent().getExtras();
  92.  
  93.        
  94.  
  95.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  96.  
  97.         mImagePSView = new ImagePSView(this, extras);
  98.  
  99.         setContentView(mImagePSView);
  100.  
  101.     }
  102.  
  103.    
  104.  
  105.     public boolean onCreateOptionsMenu(Menu menu) {
  106.  
  107.                 super.onCreateOptionsMenu(menu);
  108.  
  109.  
  110.  
  111.                 menu.add(0, MENU_SAVE, R.string.menu_save);
  112.  
  113.                 return true;
  114.  
  115.         }
  116.  
  117.  
  118.  
  119.         public boolean onOptionsItemSelected(Menu.Item item) {
  120.  
  121.                 switch (item.getId()) {
  122.  
  123.                 case MENU_SAVE:
  124.  
  125.                         OutPutPicture();
  126.  
  127.                         return true;
  128.  
  129.                 }
  130.  
  131.                 return super.onOptionsItemSelected(item);
  132.  
  133.         }
  134.  
  135.        
  136.  
  137.         public void OutPutPicture()
  138.  
  139.     {  
  140.  
  141.                 FileOutputStream fOut = null;
  142.  
  143.                 try {
  144.  
  145.                         fOut = openFileOutput("pic1.png", MODE_PRIVATE);
  146.  
  147.                 } catch (FileNotFoundException e) {
  148.  
  149.                         e.printStackTrace();
  150.  
  151.                 }
  152.  
  153.                
  154.  
  155.                 mImagePSView.mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  156.  
  157.  
  158.  
  159.         try {
  160.  
  161.                 fOut.flush();
  162.  
  163.                 } catch (IOException e) {
  164.  
  165.                         e.printStackTrace();
  166.  
  167.                 }
  168.  
  169.         try {
  170.  
  171.                 fOut.close();
  172.  
  173.                 } catch (IOException e) {
  174.  
  175.                         e.printStackTrace();
  176.  
  177.                 }      
  178.  
  179.     }
  180.  
  181. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4

:!: :!:
ImagePSView.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.imageps;
  2.  
  3.  
  4.  
  5. import java.io.File;
  6.  
  7. import java.io.FileInputStream;
  8.  
  9. import java.io.FileNotFoundException;
  10.  
  11. import java.io.FileOutputStream;
  12.  
  13. import java.io.IOException;
  14.  
  15. import java.io.OutputStream;
  16.  
  17.  
  18.  
  19. import android.content.Context;
  20.  
  21. import android.content.Resources;
  22.  
  23. import android.graphics.Bitmap;
  24.  
  25. import android.graphics.BitmapFactory;
  26.  
  27. import android.graphics.Canvas;
  28.  
  29. import android.graphics.Color;
  30.  
  31. import android.graphics.Paint;
  32.  
  33. import android.graphics.Rect;
  34.  
  35. import android.graphics.drawable.BitmapDrawable;
  36.  
  37. import android.graphics.drawable.Drawable;
  38.  
  39. import android.os.Bundle;
  40.  
  41. import android.view.KeyEvent;
  42.  
  43. import android.view.MotionEvent;
  44.  
  45. import android.view.View;
  46.  
  47.  
  48.  
  49. public class ImagePSView extends View{
  50.  
  51.  
  52.  
  53.     Bitmap mBitmap;
  54.  
  55.     Canvas mCanvas;
  56.  
  57.     private final Rect mRect = new Rect();
  58.  
  59.     private final Paint mPaint;
  60.  
  61.     private boolean mCurDown;
  62.  
  63.     private int mCurX;
  64.  
  65.     private int mCurY;
  66.  
  67.     private float mCurPressure;
  68.  
  69.     private float mCurSize;
  70.  
  71.     private int mCurWidth;
  72.  
  73.         private static final int maxEditHeight = 459;
  74.  
  75.         private static final int maxEditWidth = 320;
  76.  
  77.         private static Bitmap BitmapA;
  78.  
  79.     private static FileOutputStream fOut;
  80.  
  81.     private static BitmapDrawable bmD;
  82.  
  83.     private static int Rcolor = 0;
  84.  
  85.     private static int Gcolor = 0;
  86.  
  87.     private static int Bcolor = 0;
  88.  
  89.     int maxScrollX = 0;
  90.  
  91.     int maxScrollY = 0;
  92.  
  93.     int remainX = 0;
  94.  
  95.     int remainY = 0;
  96.  
  97.     boolean isScrollBy = false;
  98.  
  99.    
  100.  
  101.     private static int leftBound = 0;
  102.  
  103.     private static int topBound = 0;
  104.  
  105.     private static int picHeight = 0;
  106.  
  107.     private static int picWidth = 0;
  108.  
  109.    
  110.  
  111.     public ImagePSView(Context c, Bundle extras) {
  112.  
  113.         super(c);
  114.  
  115.         mPaint = new Paint();
  116.  
  117.         mPaint.setAntiAlias(true);
  118.  
  119.         mPaint.setARGB(255, 255, 255, 255);
  120.  
  121.         String path = extras.getString("filepath");
  122.  
  123.         BitmapA = BitmapFactory.decode(path);
  124.  
  125.         bmD =  new BitmapDrawable(BitmapA);
  126.  
  127.         loadPic(bmD);
  128.  
  129.     }
  130.  
  131.  
  132.  
  133.     public void loadPic(Drawable Pic) {
  134.  
  135.         picHeight = Pic.getIntrinsicHeight();
  136.  
  137.         picWidth = Pic.getIntrinsicWidth();
  138.  
  139.         leftBound = 0;
  140.  
  141.         topBound = 0;
  142.  
  143.         isScrollBy = false;
  144.  
  145.         int editWidth = picWidth;
  146.  
  147.         int editHeight = picHeight;
  148.  
  149.         isScrollBy = true;
  150.  
  151.        
  152.  
  153.         Bitmap bitmap = Bitmap.createBitmap(320, 459, true);
  154.  
  155.         Canvas canvas = new Canvas(bitmap);
  156.  
  157.        
  158.  
  159.         Pic.setBounds(0, 0, 320, 459);
  160.  
  161.         Pic.draw(canvas);
  162.  
  163.         mBitmap = bitmap;
  164.  
  165.         mCanvas = canvas;
  166.  
  167.     }
  168.  
  169.    
  170.  
  171.     @Override
  172.  
  173.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  174.  
  175.        /* int curW = mBitmap != null ? mBitmap.width() : 0;
  176.  
  177.         int curH = mBitmap != null ? mBitmap.height() : 0;
  178.  
  179.         if (curW >= w && curH >= h) {
  180.  
  181.             return;
  182.  
  183.         }
  184.  
  185.        
  186.  
  187.         if (curW < w) curW = w;
  188.  
  189.         if (curH < h) curH = h;
  190.  
  191.        
  192.  
  193.         Bitmap newBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), false);
  194.  
  195.         Canvas newCanvas = new Canvas();
  196.  
  197.         newCanvas.setDevice(newBitmap);
  198.  
  199.         if (mBitmap != null) {
  200.  
  201.             newCanvas.drawBitmap(mBitmap, 0, 0, null);
  202.  
  203.         }
  204.  
  205.         mBitmap = newBitmap;
  206.  
  207.         mCanvas = newCanvas;*/
  208.  
  209.     }
  210.  
  211.    
  212.  
  213.     @Override
  214.  
  215.     protected void onDraw(Canvas canvas) {
  216.  
  217.         if (mBitmap != null) {
  218.  
  219.             canvas.drawBitmap(mBitmap, 0, 0, null);
  220.  
  221.             //canvas.drawBitmap(BitmapA, 0, 0, null);
  222.  
  223.         }
  224.  
  225.     }
  226.  
  227.        
  228.  
  229. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4

:!: :!:
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Re: The problem of save image file

Postby NeoLiu » Wed Jan 16, 2008 6:25 pm

Hi plusminus

i have another problem

i want Add an additional content view to the screen. Added after any existing ones in the screen -- existing views are NOT removed.

but my layout is not load from layout.xml, it was created myself

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  protected void onCreate(Bundle icicle) {
  2.  
  3. super.onCreate(icicle);
  4.  
  5.  
  6.  
  7. extras = getIntent().getExtras();
  8.  
  9.  
  10.  
  11. requestWindowFeature(Window.FEATURE_NO_TITLE);
  12.  
  13. mImagePSView = new ImagePSView(this, extras);
  14.  
  15. setContentView(mImagePSView);
  16.  
  17.  
  18.  
  19. //
  20.  
  21. }
Parsed in 0.065 seconds, using GeSHi 1.0.8.4


for example i want add a dialog about edit on the bitmap, look like MapActivity dialog edit
in this view add the contents any time i want or delete it
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Re: The problem of save image file

Postby NeoLiu » Thu Jan 17, 2008 4:44 pm

:)
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests