Content providers for Images

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

Content providers for Images

Postby Rizwan » Mon Dec 31, 2007 4:59 pm

Hi!
This site has solved my almost all problems . I m about to finish my Android Contest winner project.just one thing to assemble with my code that is accessing all the images from the device . Content providers implementation as you all know there is bug in Content providers (for MIME- type media ). I m desperately looking for help/ guide to run the death script some how ............. If that runs I win. i know that.
this code did not work
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputStream is = getResources().openRawResource(R.raw.sample_0);
  2.  
  3.     bm = BitmapFactory.decodeStream(is);
  4.  
  5.         ContentValues values  = new ContentValues();//= new
  6.  
  7.         values.put(Media.NAME, "road_trip_1");
  8.  
  9.         values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
  10.  
  11.         ContentURI uri =
  12.  
  13. getContentResolver().insert(Media.INTERNAL_CONTENT_URI, values);
  14.  
  15.         OutputStream outStream;
  16.  
  17.         try {
  18.  
  19.                 outStream =  getContentResolver().openOutputStream(uri);
  20.  
  21.                 bm.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
  22.  
  23.         outStream.close();
  24.  
  25.         } catch (FileNotFoundException e) {
  26.  
  27.                 text.append(e.toString());
  28.  
  29.         }catch(Exception e){
  30.  
  31.                 text.append(e.toString()) ;
  32.  
  33.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

official Google documented code is dead
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Save the name and description in a map. Key is the content provider's
  2. // column name, value is the value to save in that record field.
  3. HashMap<String, Object> values = new HashMap<String, Object>();
  4. values.put(Media.Images.NAME, "road_trip_1");
  5. values.put(Media.Images.DESCRIPTION, "Day 1, trip to Los Angeles");
  6.  
  7. // Add a new record without the bitmap, but with the values.
  8. // It returns the URI of the new record.
  9. ContentURI uri = getContentResolver().insert(Media.Images.CONTENT_URI, values);
  10.  
  11. // Now get a handle to the file for that record, and save the data into it.
  12. // sourceBitmap is a Bitmap object representing the file to save to the database.
  13. OutputStream outStream = getContentResolver.openOutputStream(uri);
  14. sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
  15. outStream.close();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Or, using a convenience class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. android.provider.Media.Images.insertImage( getContentResolver(),
  2.                                            sourceBitmap,
  3.                                            "road_trip_1",
  4.                                            "Day 1, trip to Los Angeles");
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



please let me know the solution of this problem
Rizwan
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Dec 16, 2007 6:35 pm

Top

Postby Rizwan » Sun Jan 06, 2008 4:30 pm

Hi!
Can anybody help me in solving that problem or i have to write my own custom provider for that ?
Rizwan
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Dec 16, 2007 6:35 pm

Postby plusminus » Sun Jan 06, 2008 7:13 pm

Hello Rizwan,

which of the codes above are you actually using and what is the problem :?:

(btw, with me it is:
android.provider.MediaStore.Images.Media.insertImage(this.getContentResolver(), sourceBitmap , "a name", "a description");
)

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

Postby Rizwan » Mon Jan 07, 2008 9:21 am

yes you r rite or even if you query for content://Media/Images you will get this exception
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. E/AndroidRuntime(518): java.lang.UnsupportedOperationException:
  2.  
  3. Unknown URI: content://media/images
  4.  
  5. E/AndroidRuntime(518): at
  6.  
  7. com.google.android.providers.media.MediaProvider.query(MediaProvider.java:235)
  8.  
  9. E/AndroidRuntime(518): at android.content.ContentProvider
  10.  
  11. $Transport.bulkQuery(ContentProvider.java:129)
  12.  
  13. E/AndroidRuntime(518): at
  14.  
  15. android.content.ContentProviderNative.onTransact(ContentProviderNative.java­:90)
  16.  
  17. E/AndroidRuntime(518): at
  18.  
  19. android.os.BinderNative.execTransact(BinderNative.java:194)
  20.  
  21. E/AndroidRuntime(518): at android.dalvik.NativeStart.run(Native Method)
  22.  
  23. E/JavaBinder(518): *** Uncaught remote exception!  (Exceptions are not...  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



Looking forward for ur reply
Rizwan
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sun Dec 16, 2007 6:35 pm

Postby zjshb » Tue Jan 08, 2008 3:02 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. E/AndroidRuntime(564): at android.dalvik.NativeStart.run(Native Method)
  2.  
  3. E/JavaBinder(564): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
  4.  
  5. E/JavaBinder(564): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media
  6.  
  7. E/JavaBinder(564): at com.google.android.providers.media.MediaProvider.insert(MediaProvider.java:437)
  8.  
  9. E/JavaBinder(564): at android.content.ContentProvider$Transport.insert(ContentProvider.java:155)
  10.  
  11. E/JavaBinder(564): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:116)
  12.  
  13. E/JavaBinder(564): at android.os.BinderNative.execTransact(BinderNative.java:194)
  14.  
  15. E/JavaBinder(564): at android.dalvik.NativeStart.run(Native Method)
  16.  
  17. E/Log(587): Saving exception to database.
  18.  
  19. E/AndroidRuntime(587): java.lang.NullPointerException
  20.  
  21. E/AndroidRuntime(587): at android.content.ContentResolver.getProvider(ContentResolver.java:318)
  22.  
  23. E/AndroidRuntime(587): at android.content.ContentResolver.query(ContentResolver.java:89)
  24.  
  25. E/AndroidRuntime(587): at android.content.ContentResolver.getDataFileHack(ContentResolver.java:190)
  26.  
  27. E/AndroidRuntime(587): at android.content.ContentResolver.openOutputStream(ContentResolver.java:169)
  28.  
  29. E/AndroidRuntime(587): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:165)
  30.  
  31. E/AndroidRuntime(587): at com.google.android.helloactivity.HelloActivity$1.onClick(HelloActivity.java:50)
  32.  
  33. E/AndroidRuntime(587): at android.view.View.performClick(View.java:1515)
  34.  
  35. E/AndroidRuntime(587): at android.widget.Button.performClick(Button.java:148)
  36.  
  37. E/AndroidRuntime(587): at android.widget.Button.onMotionEvent(Button.java:102)
  38.  
  39. E/AndroidRuntime(587): at android.view.View.dispatchMotionEvent(View.java:2222)
  40.  
  41. E/AndroidRuntime(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  42.  
  43. E/AndroidRuntime(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  44.  
  45. E/AndroidRuntime(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  46.  
  47. E/AndroidRuntime(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  48.  
  49. E/AndroidRuntime(587): at android.view.ViewRoot.handleMessage(ViewRoot.java:613)
  50.  
  51. E/AndroidRuntime(587): at android.os.Handler.dispatchMessage(Handler.java:80)
  52.  
  53. E/AndroidRuntime(587): at android.os.Looper.loop(Looper.java:71)
  54.  
  55. E/AndroidRuntime(587): at android.app.ActivityThread.main(ActivityThread.java:2506)
  56.  
  57. E/AndroidRuntime(587): at java.lang.reflect.Method.invokeNative(Native Method)
  58.  
  59. E/AndroidRuntime(587): at java.lang.reflect.Method.invoke(Method.java:380)
  60.  
  61. E/AndroidRuntime(587): at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1170)
  62.  
  63. E/AndroidRuntime(587): at android.os.ZygoteInit.main(ZygoteInit.java:1121)
  64.  
  65. E/AndroidRuntime(587): at android.dalvik.NativeStart.main(Native Method)
  66.  
  67. E/MediaStore(587): Failed to insert image
  68.  
  69. E/MediaStore(587): java.lang.NullPointerException
  70.  
  71. E/MediaStore(587): at android.content.ContentResolver.getProvider(ContentResolver.java:318)
  72.  
  73. E/MediaStore(587): at android.content.ContentResolver.query(ContentResolver.java:89)
  74.  
  75. E/MediaStore(587): at android.content.ContentResolver.getDataFileHack(ContentResolver.java:190)
  76.  
  77. E/MediaStore(587): at android.content.ContentResolver.openOutputStream(ContentResolver.java:169)
  78.  
  79. E/MediaStore(587): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:165)
  80.  
  81. E/MediaStore(587): at com.google.android.helloactivity.HelloActivity$1.onClick(HelloActivity.java:50)
  82.  
  83. E/MediaStore(587): at android.view.View.performClick(View.java:1515)
  84.  
  85. E/MediaStore(587): at android.widget.Button.performClick(Button.java:148)
  86.  
  87. E/MediaStore(587): at android.widget.Button.onMotionEvent(Button.java:102)
  88.  
  89. E/MediaStore(587): at android.view.View.dispatchMotionEvent(View.java:2222)
  90.  
  91. E/MediaStore(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  92.  
  93. E/MediaStore(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  94.  
  95. E/MediaStore(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  96.  
  97. E/MediaStore(587): at android.view.ViewGroup.dispatchMotionEvent(ViewGroup.java:456)
  98.  
  99. E/MediaStore(587): at android.view.ViewRoot.handleMessage(ViewRoot.java:613)
  100.  
  101. E/MediaStore(587): at android.os.Handler.dispatchMessage(Handler.java:80)
  102.  
  103. E/MediaStore(587): at android.os.Looper.loop(Looper.java:71)
  104.  
  105. E/MediaStore(587): at android.app.ActivityThread.main(ActivityThread.java:2506)
  106.  
  107. E/MediaStore(587): at java.lang.reflect.Method.invokeNative(Native Method)
  108.  
  109. E/MediaStore(587): at java.lang.reflect.Method.invoke(Method.java:380)
  110.  
  111. E/MediaStore(587): at android.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1170)
  112.  
  113. E/MediaStore(587): at android.os.ZygoteInit.main(ZygoteInit.java:1121)
  114.  
  115. E/MediaStore(587): at android.dalvik.NativeStart.main(Native Method)
Parsed in 0.053 seconds, using GeSHi 1.0.8.4
zjshb
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 08, 2008 2:58 am

Image save but nullpointer on view

Postby mledom » Wed Feb 11, 2009 9:20 pm

Have you tried to see if it works when you run it on the phone? I think if you don't have the "SDCard" setup for the emulator you will get that exception. I was and I decided to run it on the phone to see if it works and it does.

I used the Media.insertImage method and was able to save an image. There problem now is that everytime I go to view my pictures I get a dialog message that says "Sorry The application Camera has stopped unexpectedly...". If I restart the phone I can see the image in picture dir. As soon as I insert another image I get camera crash dialog.
mledom
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 11, 2009 9:14 pm

Top

kindly solve my android problem

Postby sanat2003 » Mon Feb 16, 2009 10:11 am

package org.find.place;



import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class mapapplication extends Activity {

/** Called when the activity is first created. */

private EditText lat;
private EditText lon;
WebView browser;

@Override
public void onCreate(Bundle SavedInstance) {
super.onCreate(SavedInstance);
setContentView(R.layout.main);

Button btn =(Button)findViewById(R.id.map);
lat=(EditText)findViewById(R.id.lat);
lon=(EditText)findViewById(R.id.lon);

btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){


double x = mapapplication.calcx(lat);
double y = mapapplication.calcy(lon);

browser=(WebView) findViewById(R.id.webkit);
browser.loadUrl("http://app.mapmyindia.com/mobapp/getMap.php?x=" + x + "&y=" + y + "&level=12&width=200&height=200");


// String uriString ="browser";

//startActivity(new Intent(Intent.ACTION_VIEW,uriString));

}

}); }


public static double calcx(EditText latitude) {
double tempLat = Double.parseDouble(latitude.toString());
double sxne = 6999999.98;
double sxwg84 = 62.9525;
double _lat =(tempLat*sxne)/sxwg84;
double y=4096+(4521000-_lat)/1000;
tempLat = (4521000-(y-4096)*1000)+3000000;
return tempLat;
}
public static double calcy(EditText longitude){
double tempLon = Double.parseDouble(longitude.toString());
double syne = 425000.04;
double sywgs84 = 3.82212;
double _lon =(tempLon*syne)/sywgs84;
double x=4096+(_lon-6999999)/1000;
tempLon = (4521000-(x-4096)*1000)+10000000;
return tempLon;
}




}



pls help me out
sanat2003
Freshman
Freshman
 
Posts: 4
Joined: Thu Dec 04, 2008 10:44 am

Unknown URI: content://media/external/images/media

Postby Dayn » Mon Aug 24, 2009 3:58 pm

I have the same error:

but it's the same code than: content-providers

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.OutputStream;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.ContentValues;
  9.  
  10. import android.graphics.Bitmap;
  11.  
  12. import android.graphics.BitmapFactory;
  13.  
  14. import android.net.Uri;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.provider.MediaStore.Images.Media;
  19.  
  20. import android.util.Log;
  21.  
  22.  
  23.  
  24. public class testjpeg extends Activity {
  25.  
  26.     private static final String TAG = null;
  27.  
  28.  
  29.  
  30.         /** Called when the activity is first created. */
  31.  
  32.     @Override
  33.  
  34.     public void onCreate(Bundle savedInstanceState) {
  35.  
  36.         super.onCreate(savedInstanceState);
  37.  
  38.         setContentView(R.layout.main);
  39.  
  40.        
  41.  
  42.      // Save the name and description of an image in a ContentValues map.  
  43.  
  44.         ContentValues values = new ContentValues(3);
  45.  
  46.         values.put(Media.DISPLAY_NAME, "road_trip_1");
  47.  
  48.         values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
  49.  
  50.         values.put(Media.MIME_TYPE, "image/jpeg");
  51.  
  52.  
  53.  
  54.         // Add a new record without the bitmap, but with the values just set.
  55.  
  56.         // insert() returns the URI of the new record.
  57.  
  58.        
  59.  
  60.         Log.v(TAG, Media.EXTERNAL_CONTENT_URI.toString());
  61.  
  62.  
  63.  
  64.         Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
  65.  
  66.  
  67.  
  68.         // Now get a handle to the file for that record, and save the data into it.
  69.  
  70.         // Here, sourceBitmap is a Bitmap object representing the file to save to the database.
  71.  
  72.              
  73.  
  74.         Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
  75.  
  76.        
  77.  
  78.         try {
  79.  
  80.             OutputStream outStream = getContentResolver().openOutputStream(uri);
  81.  
  82.             sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
  83.  
  84.             outStream.close();
  85.  
  86.         } catch (Exception e) {
  87.  
  88.             Log.e(TAG, "exception while writing image: ", e);
  89.  
  90.         }        
  91.  
  92.        
  93.  
  94.     }
  95.  
  96. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Why I have this error and what I'm supposed to fix that ??

Dayn
User avatar
Dayn
Developer
Developer
 
Posts: 31
Joined: Thu Jul 09, 2009 10:45 am
Location: Paris and Dijon

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests