2013-03-06 17 views
16

Mam aktywność, którą daje użytkownikowi opcję kliknięcia obrazu z kamery, a następnie zapisuję ten obraz w tablicy bajtów i w bazie danych. Jednak mój kod nie wydają się działać na Samsung Galaxy S3 poniżej kod:Aparat nie działa z Samsung Galaxy S3

Camera nazywając intencją:

if (i == 0) { 
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
} 

na metodzie aktywny do kamery:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1337 && resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     if (extras != null) { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      thumbnail = (Bitmap) extras.get("data"); 
      image(thumbnail); 
     } else { 
      Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show(); 
     } 

     super.onActivityResult(requestCode, resultCode, data);  
    } 
} 

mojego obrazu (bitmapy miniatura) funkcja:

public void image(Bitmap thumbnail) { 
    Bitmap photo = thumbnail; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
    b = bos.toByteArray(); 
    ImageView imageview = (ImageView)findViewById(R.id.imageView1); 
    Bitmap bt = Bitmap.createScaledBitmap(photo, 100, 80, false); 
    imageview.setImageBitmap(bt); 
} 

Jednak to nie działało z Samsung S3, zmieniłem c Od na poniższe i teraz działa z Samsung S3, jednak nie działa z żadnym innym urządzeniem.

Camera zamiar:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

aktywny dla wyniku:

startActivityForResult(intent, CAMERA_IMAGE_CAPTURE); 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { 
     // Describe the columns you'd like to have returned. Selecting from the Thumbnails location gives you both the Thumbnail Image ID, as well as the original image ID 
String[] projection = { 
MediaStore.Images.Thumbnails._ID, // The columns we want 
MediaStore.Images.Thumbnails.IMAGE_ID, 
MediaStore.Images.Thumbnails.KIND, 
MediaStore.Images.Thumbnails.DATA}; 
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's 
MediaStore.Images.Thumbnails.MINI_KIND; 

String sort = MediaStore.Images.Thumbnails._ID + " DESC"; 

//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable 
Cursor myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort); 

long imageId = 0l; 
long thumbnailImageId = 0l; 
String thumbnailPath = ""; 

try{ 
myCursor.moveToFirst(); 
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID)); 
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID)); 
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); 
} 
finally{myCursor.close();} 

//Create new Cursor to obtain the file Path for the large image 

String[] largeFileProjection = { 
MediaStore.Images.ImageColumns._ID, 
MediaStore.Images.ImageColumns.DATA 
}; 

String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; 
myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort); 
String largeImagePath = ""; 

try{ 
myCursor.moveToFirst(); 

//This will actually give yo uthe file path location of the image. 
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); 
} 
finally{myCursor.close();} 
// These are the two URI's you'll be interested in. They give you a handle to the actual images 
Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId)); 
Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId)); 

// I've left out the remaining code, as all I do is assign the URI's to my own objects anyways... 
// Toast.makeText(this, ""+largeImagePath, Toast.LENGTH_LONG).show(); 
// Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show(); 
// Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show(); 

    if (largeImagePath != null) { 
     // Toast.makeText(this, "" + largeImagePath, Toast.LENGTH_LONG).show(); 

     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inSampleSize = OG; 
     // thumbnail = (BitmapFactory.decodeFile(picturePath)); 
     thumbnail = BitmapFactory.decodeFile((largeImagePath), opts); 
     System.gc(); 
     if (thumbnail != null) { 
      Toast.makeText(this, "Success", Toast.LENGTH_LONG).show(); 
     } 

     image(thumbnail); 

    } 

    if (uriLargeImage != null) { 
     Toast.makeText(this, "" + uriLargeImage, Toast.LENGTH_LONG).show(); 
    } 

    if (uriThumbnailImage != null) { 
     Toast.makeText(this, "" + uriThumbnailImage, Toast.LENGTH_LONG).show(); 
    } 
} 

To jest mój obraz() funkcja:

public void image(Bitmap thumbnail) { 
    b = null; 

    Bitmap photo = thumbnail; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    photo.compress(Bitmap.CompressFormat.PNG, 100, bos); 
    b = bos.toByteArray(); 

    if (b != null) { 
     Toast.makeText(this, "Success Yeah" + b, Toast.LENGTH_LONG).show(); 
    } 
} 

Podczas gdy wszystkie trzy 1) uriLargeImage 2) largeImagePath 3) uriThumbnailImage zwraca mi ścieżkę lub URI Nie mogę ustawić utworzonej mapy bitowej do mojego ImageView. Jednak tak jest tylko w przypadku Samsung S3, jeśli uruchomię powyższy kod z jakimkolwiek innym urządzeniem, program się zawiesi.

W manifeście Użyłem

android:configChanges="keyboardHidden|orientation" 

podstawie tutorialu: http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/

Jednak gdybym zrobić zdjęcie w trybie krajobrazu, wszystko działa bez zarzutu! Jestem zdziwiony!! (W Samsung S3)

+0

Mam ten sam problem z tym konkretnym urządzeniem. Rozwiązałem ten problem po wielu metach prób i błędów. możesz uzyskać lepszy pomysł na ten temat od [tutaj] (http://stackoverflow.com/questions/14495304/camera-force-closing-issue-in-samsung-galaxy-s3-version-4-1-1/14640678 # 14640678) –

+0

Powyższe odwołanie jest tym samym kumplem, wziąłem odwołanie z tego samego samouczka, podczas gdy wszystko wydaje się działać dobrze, jedynym problemem jest to, że tracę dane gdzieś, co nie powinno się zdarzyć z manifestem manifestu. – Skynet

+0

Czy chcesz połączyć swój projekt? Nie miałbym nic przeciwko testowaniu niektórych rzeczy. –

Odpowiedz

16

końcu po zmaga się przez trzy dni, opracowali prosty mechanizm do przezwyciężenia Saga Samsung S3.

Poniżej znajduje się kod, szybkie podsumowanie kodu jest to, że najpierw sprawdzić producenta urządzenia oraz w oparciu o to nazywam różne intencje dla kamery.

public class MainActivity extends Activity { 

    private static int RESULT_LOAD_IMAGE = 1; 
    private static final int PICK_FROM_GALLERY = 2; 
    int CAMERA_PIC_REQUEST = 1337; 
    Bitmap thumbnail = null; 
    private static final int OG = 4; 

    private static final int CAMERA_IMAGE_CAPTURE = 0; 
    Uri u; 
    ImageView imgview; 
    // int z=0; 
    String z = null; 
    byte b[]; 
    String largeImagePath = ""; 
    Uri uriLargeImage; 
    Uri uriThumbnailImage; 
    Cursor myCursor; 

    public void imageCam(Bitmap thumbnail) { 
     Bitmap photo = thumbnail; 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     photo.compress(Bitmap.CompressFormat.JPEG, 70, bos); 
     b = bos.toByteArray(); 
     ImageView imageview = (ImageView) findViewById(R.id.imageView1); 
     imageview.setImageBitmap(photo); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button bt = (Button) findViewById(R.id.button1); 
     bt.setOnClickListener(onBTN); 
     if (savedInstanceState != null) { 

      Bitmap Zatang; 
      String B1 = savedInstanceState.getString("message"); 
      Toast.makeText(this, "SavedYeah" + B1, Toast.LENGTH_LONG).show(); 
      BitmapFactory.Options opts = new BitmapFactory.Options(); 
      opts.inSampleSize = OG; 
      Zatang = BitmapFactory.decodeFile((B1), opts); 
      System.gc(); 
      if (Zatang != null) { 
       Toast.makeText(this, "Success Zatang" + B1, Toast.LENGTH_LONG) 
       .show(); 
       imageCam(Zatang); 
      } 
     } 
    } 

    private View.OnClickListener onBTN = new View.OnClickListener() { 
     public void onClick(View v) { 
      openNewGameDialog(); 
     } 
    }; 

    String[] B = { "Cam", "Gallery" }; 

    private void openNewGameDialog() { 
     new AlertDialog.Builder(this).setItems(B,new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialoginterface,int i) { 
       if (i == 0) { 
        String BX1 = android.os.Build.MANUFACTURER; 

        if(BX1.equalsIgnoreCase("samsung")) { 
         Toast.makeText(getApplicationContext(), "Device man"+BX1, Toast.LENGTH_LONG).show(); 
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(intent, CAMERA_IMAGE_CAPTURE); 

        } else { 
         Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
        } 
       } else if (i == 1) { 
        Intent intent = new Intent(Intent.ACTION_PICK); 
        intent.setType("image/*"); 
        startActivityForResult(intent, RESULT_LOAD_IMAGE); 
       } 
      }).show(); 
     } 

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){ 

       // Describe the columns you'd like to have returned. Selecting from the Thumbnails location gives you both the Thumbnail Image ID, as well as the original image ID 
       String[] projection = { 
         MediaStore.Images.Thumbnails._ID, // The columns we want 
         MediaStore.Images.Thumbnails.IMAGE_ID, 
         MediaStore.Images.Thumbnails.KIND, 
         MediaStore.Images.Thumbnails.DATA}; 
       String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's 
         MediaStore.Images.Thumbnails.MINI_KIND; 

       String sort = MediaStore.Images.Thumbnails._ID + " DESC"; 

       //At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable 
       myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort); 

       long imageId = 0l; 
       long thumbnailImageId = 0l; 
       String thumbnailPath = ""; 

       try { 
        myCursor.moveToFirst(); 
        imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID)); 
        thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID)); 
        thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); 
       } finally { 
        myCursor.close(); 
       } 

       //Create new Cursor to obtain the file Path for the large image 

       String[] largeFileProjection = { 
         MediaStore.Images.ImageColumns._ID, 
         MediaStore.Images.ImageColumns.DATA 
       }; 

       String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; 
       myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort); 
       largeImagePath = ""; 

       try { 
        myCursor.moveToFirst(); 

        //This will actually give yo uthe file path location of the image. 
        largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); 
       } finally { 
        myCursor.close(); 
       } 
       // These are the two URI's you'll be interested in. They give you a handle to the actual images 
       uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId)); 
       uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId)); 

       // I've left out the remaining code, as all I do is assign the URI's to my own objects anyways... 
       // Toast.makeText(this, ""+largeImagePath, Toast.LENGTH_LONG).show(); 
       // Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show(); 
       // Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show(); 


       if (largeImagePath != null) { 
        Toast.makeText(this, "LARGE YES"+largeImagePath, Toast.LENGTH_LONG).show(); 

        BitmapFactory.Options opts = new BitmapFactory.Options(); 
        opts.inSampleSize = OG; 
        thumbnail = BitmapFactory.decodeFile((largeImagePath), opts); 
        System.gc(); 
        if (thumbnail != null) { 
         Toast.makeText(this, "Try Without Saved Instance", Toast.LENGTH_LONG).show(); 
         imageCam(thumbnail); 
        } 
       } 
       if (uriLargeImage != null) { 
        Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show(); 
       } 
       if (uriThumbnailImage != null) { 
        Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show(); 
       } 
      } 
      if(requestCode == 1337 && resultCode== RESULT_OK){ 
       Bundle extras = data.getExtras(); 
       if (extras.keySet().contains("data")){ 
        BitmapFactory.Options options = new BitmapFactory.Options(); 
        thumbnail = (Bitmap) extras.get("data"); 
        if (thumbnail != null) { 
         Toast.makeText(this, "YES Thumbnail", Toast.LENGTH_LONG).show(); 
         BitmapFactory.Options opt = new BitmapFactory.Options(); 
         thumbnail = (Bitmap) extras.get("data"); 
         imageCam(thumbnail); 
        } 
       } else { 
        Uri imageURI = getIntent().getData(); 
        ImageView imageview = (ImageView)findViewById(R.id.imageView1); 
        imageview.setImageURI(imageURI); 

        if(imageURI != null){ 
         Toast.makeText(this, "YES Image Uri", Toast.LENGTH_LONG).show(); 
        } 
       } 
       super.onActivityResult(requestCode, resultCode, data); 
      } 
      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
       Uri selectedImage = data.getData(); 

       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
       Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String picturePath = cursor.getString(columnIndex); 
       cursor.close(); 
       BitmapFactory.Options opts = new BitmapFactory.Options(); 
       thumbnail = BitmapFactory.decodeFile((picturePath), opts); 
       System.gc(); 
       imageCam(thumbnail);    
      } 
     } 
     @Override 
     public void onSaveInstanceState(Bundle outState) { 
      super.onSaveInstanceState(outState); 
      outState.putString("message",largeImagePath); 
     } 

    } 
} 
+0

Drogi, dziękuję, podziel się swoim rozwiązaniem !! Zależy od Ciebie! – Mateus

+0

Witaj kolego :) – Skynet

+0

Działa dobrze z Samsung Galaxy S3. Ale nie dla wszystkich urządzeń Samsung. – redestructa

Powiązane problemy