SORU
17 HAZİRAN 2011, Cuma


Kamera niyet sonra Galeri resim silme fotoğrafı

Bu çok farklı şekillerde soruldu biliyorum ama ben hala varsayılan klasöründen Galerisi resmi sil olabilir. SD kart doğru dosyayı saklıyorum ve bu dosya iyi silebilirim, ama varsayılan galeriye resim klasörü Kameranın altında gösterir silmez O dosya.

Resim etkinliği dosyası zaten /Coupon2 altında SD kartta saklanır bu yana döndüğünde silmek istiyorum.

Herhangi bir öneriniz var mı?

public void startCamera() {
    Log.d("ANDRO_CAMERA", "Starting camera on the phone...");

    mManufacturerText = (EditText) findViewById(R.id.manufacturer);
    String ManufacturerText = mManufacturerText.getText().toString();
    String currentDateTimeString = new Date().toString();

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File filedir = new File(Environment.getExternalStorageDirectory() "/Coupon2");
    filedir.mkdirs();

    File file = new File(Environment.getExternalStorageDirectory() "/Coupon2", ManufacturerText "-test.png");
    outputFileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == -1) {  
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.putExtra("crop", "true");
        intent.putExtra("scale", "true");

        intent.putExtra("return-data", false);
        intent.setDataAndType(outputFileUri, "image/*");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, CAMERA_CROP_REQUEST);
    }else { 
        SetImage();
        saveState();
    }
}

CEVAP
6 Temmuz 2011, ÇARŞAMBA


Benim uygulama bana bir fotoğraf çekmek için bir niyet aramak gerekir. Fotoğraf Galerisi olamaz ama onun yerine SD kart üzerinde belirli bir dizinde olması gerekir.

Aslında ben EXTRA_OUTPUT, ama ben yakında aşağıdaki keşfedilen sadece kullanılan: - Bazı cihazlar tamamen kullanım ve galeri atlayın. - Bazı cihazlar tamamen yok sayıp SADECE Galeri kullanın. - Bazı cihazlar gerçekten berbat ve galeri için tam boyutlu görüntü kaydetmek ve sadece istediğim yere bir küçük resim Kaydet. (Sen kim olduğunu...) HTC

Yani, körü körüne işim bittiğinde Galeri bir dosyayı silemiyorum. Son eklenen fotoğraf olabilir veya kaldırmak istiyorum olmayabilir. Ayrıca, bu dosya daha sonra kendi yerine kopyasını verebilirim. Benim etkinlik 2000 satır ve benim şirketimiz tüm kod yazan olmak istemem çünkü tek yöntem bu işin içinde yer post ediyorum. Umarım bu yardımcı olur.

Ayrıca, devlet edeceğim, bu benim ilk Android uygulaması. Eğer sadece, bilmem ki bunu yapmanın daha iyi bir yolu varsa hiç şaşırmam, ama bu benim için çalışıyor!

Yani, burada benim çözüm:

İlk uygulamam bağlamda ben aşağıdaki gibi bir değişken tanımlayın

public ArrayList<String> GalleryList = new ArrayList<String>();

Gelecek, benim aktivitede, ben galerideki tüm fotoğrafları listesini almak için bir yöntem tanımlar:

private void FillPhotoList()
{
   // initialize the list!
   app.GalleryList.clear();
   String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME };
   // intialize the Uri and the Cursor, and the current expected size.
   Cursor c = null; 
   Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
   //
   // Query the Uri to get the data path.  Only if the Uri is valid.
   if (u != null)
   {
      c = managedQuery(u, projection, null, null, null);
   }

   // If we found the cursor and found a record in it (we also have the id).
   if ((c != null) && (c.moveToFirst())) 
   {
      do 
      {
        // Loop each and add to the list.
        app.GalleryList.add(c.getString(0));
      }     
      while (c.moveToNext());
   }
}

İşte yeni imajım için benzersiz bir dosya adını döndürmek için bir yöntem:

private String getTempFileString()
{
   // Only one time will we grab this location.
   final File path = new File(Environment.getExternalStorageDirectory(), 
         getString(getApplicationInfo().labelRes));
   //
   // If this does not exist, we can create it here.
   if (!path.exists())
   {
      path.mkdir();
   }
   //
   return new File(path, String.valueOf(System.currentTimeMillis())   ".jpg").getPath();
}

Geçerli bir dosya hakkında bana bilgi deposu benim aktivitede üç değişken var. Bu dosyaya bir dize (path) Dosya değişkeni ve bir URI:

public static String sFilePath = ""; 
public static File CurrentFile = null;
public static Uri CurrentUri = null;

Asla doğrudan bu set, sadece dosya yolu üzerinde bir pasör çağrı:

public void setsFilePath(String value)
{
   // We just updated this value. Set the property first.
   sFilePath = value;
   //
   // initialize these two
   CurrentFile = null;
   CurrentUri = null;
   //
   // If we have something real, setup the file and the Uri.
   if (!sFilePath.equalsIgnoreCase(""))
   {
      CurrentFile = new File(sFilePath);
      CurrentUri = Uri.fromFile(CurrentFile);
   }
}

Şimdi bir fotoğraf çekmek için bir niyet ararım.

public void startCamera()
{
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   // Specify the output. This will be unique.
   setsFilePath(getTempFileString());
   //
   intent.putExtra(MediaStore.EXTRA_OUTPUT, CurrentUri);
   //
   // Keep a list for afterwards
   FillPhotoList();
   //
   // finally start the intent and wait for a result.
   startActivityForResult(intent, IMAGE_CAPTURE);
}

Bu yapılır ve aktivite geri dönünce, burada benim kod:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   if (requestCode == IMAGE_CAPTURE)
   {
      // based on the result we either set the preview or show a quick toast splash.
      if (resultCode == RESULT_OK)
      {
         // This is ##### ridiculous.  Some versions of Android save
         // to the MediaStore as well.  Not sure why!  We don't know what
         // name Android will give either, so we get to search for this
         // manually and remove it.  
         String[] projection = { MediaStore.Images.ImageColumns.SIZE,
                                 MediaStore.Images.ImageColumns.DISPLAY_NAME,
                                 MediaStore.Images.ImageColumns.DATA,
                                 BaseColumns._ID,};
         //    
         // intialize the Uri and the Cursor, and the current expected size.
         Cursor c = null; 
         Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
         //
         if (CurrentFile != null)
         {               
            // Query the Uri to get the data path.  Only if the Uri is valid,
            // and we had a valid size to be searching for.
            if ((u != null) && (CurrentFile.length() > 0))
            {
               c = managedQuery(u, projection, null, null, null);
            }
            //   
            // If we found the cursor and found a record in it (we also have the size).
            if ((c != null) && (c.moveToFirst())) 
            {
               do 
               {
                  // Check each area in the gallary we built before.
                  boolean bFound = false;
                  for (String sGallery : app.GalleryList)
                  {
                     if (sGallery.equalsIgnoreCase(c.getString(1)))
                     {
                        bFound = true;
                        break;
                     }
                  }
                  //       
                  // To here we looped the full gallery.
                  if (!bFound)
                  {
                     // This is the NEW image.  If the size is bigger, copy it.
                     // Then delete it!
                     File f = new File(c.getString(2));

                     // Ensure it's there, check size, and delete!
                     if ((f.exists()) && (CurrentFile.length() < c.getLong(0)) && (CurrentFile.delete()))
                     {
                        // Finally we can stop the copy.
                        try
                        {
                           CurrentFile.createNewFile();
                           FileChannel source = null;
                           FileChannel destination = null;
                           try 
                           {
                              source = new FileInputStream(f).getChannel();
                              destination = new FileOutputStream(CurrentFile).getChannel();
                              destination.transferFrom(source, 0, source.size());
                           }
                           finally 
                           {
                              if (source != null) 
                              {
                                 source.close();
                              }
                              if (destination != null) 
                              {
                                 destination.close();
                              }
                           }
                        }
                        catch (IOException e)
                        {
                           // Could not copy the file over.
                           app.CallToast(PhotosActivity.this, getString(R.string.ErrorOccured), 0);
                        }
                     }
                     //       
                     ContentResolver cr = getContentResolver();
                     cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                        BaseColumns._ID   "="   c.getString(3), null);
                     break;                        
                  }
               } 
               while (c.moveToNext());
            }
         }
      }
   }      
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Bucky Roberts

    Bucky Robert

    9 HAZİRAN 2011
  • guillaume2111's channel

    guillaume211

    19 Kasım 2006
  • Richard Laxa

    Richard Laxa

    30 AĞUSTOS 2012