2010-12-30 4 views
1

사진 폴더에서 r.drawable.image1의 이미지를 휴대 전화의 SD 카드에 저장 한 다음 MediaScanner로 브로드 캐스트를 보내면 새로 고침하는 데 문제가 있습니다. 이미지가 갤러리의 갤러리에 표시되도록 폴더를 엽니 다.Android 도움말에서 Sd 카드에 이미지 저장 및 MediaScanner로 SendBroadcast 보내기

나는 솔루션이 함께 할 수있는 뭔가입니다 믿습니다이

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 

문제는 이제 이미지가 SD에 저장하지 않고 코드와 미디어 스캐너 위의 코드를 넣어 어디 몰라 아래의 3 일간은 지난 3 일간 그걸 가지고 놀랐다. ...

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import android.net.Uri; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.widget.Button; 
import android.widget.Toast; 
import android.media.MediaScannerConnection; 
import android.os.Bundle; 
import android.os.Environment; 

public class MainScreen extends Activity 
{ 
    private Button btnDownload; 
    /** Called when the activity is first created. */ 
    @Override  
    public void onCreate(Bundle savedInstanceState) 
    {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.main);  
     btnDownload=(Button)findViewById(R.id.SaveButton1); 

     btnDownload.setOnClickListener(
      new Button.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String newPicture = saveToSDCard(R.drawable.image1, "image1.jpg"); 
        startMediaScanner(newPicture); 
       } 
      } 
     ); 
    } 

    private String saveToSDCard(int resourceID, String finalName) 
    { 
     StringBuffer createdFile = new StringBuffer(); 

     Bitmap resourceImage = BitmapFactory.decodeResource(this.getResources(), resourceID);  
     File externalStorageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), finalName); 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     resourceImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     byte b[] = bytes.toByteArray(); 

     try 
     { 
      externalStorageFile.createNewFile(); 
      OutputStream filoutputStream = new FileOutputStream(externalStorageFile); 
      filoutputStream.write(b); 
      filoutputStream.flush(); 
      filoutputStream.close(); 
      createdFile.append(externalStorageFile.getAbsolutePath()); 
     } 
     catch (IOException e) 
     { 
     } 

     return createdFile.toString(); 
    } 

    private void startMediaScanner(String fileName) 
    { 
     MediaScannerConnection.scanFile(this, 
      new String[] { fileName }, null, 
      new MediaScannerConnection.OnScanCompletedListener() { 
      public void onScanCompleted(String path, Uri uri) 
      { 
       Toast.makeText(MainScreen.this, "Media scan Completed", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 
+0

원본 답변을 업데이트했습니다. (http://stackoverflow.com/questions/4509877/android-dev-help-saving-an-image-from-res-raw-or-asset-folder-to-the- sd-card/4512973 # 4512973) 나는 당신에게 준, 그래서 지금은 제대로 작동합니다. 나는 내 전화로 그것을 시도했다. 그것은 당신이 직면 한 MediaScanner와 관련된 문제를 해결합니다. – Zelimir

답변

0

"이미지가 전화 갤러리에 나타나도록 폴더를 새로 고치려면"MediaScannerConnection을 사용하십시오 제발 도와주세요.

+0

안녕하세요 초심자가되어 죄송합니다. 어떻게해야합니까? 나는 이미 더 이상 sd에 저장하지 못한다는 점에서 위의 코드로 이미 엉망으로 만들었습니다. 완전한 코드를 보여 주시면 감사하겠습니다. Thankyou, Lucy – Lucy

+0

@Lucy : 나는'MediaScannerConnection'을 사용하지 않았지만'new MediaScannerConnection (this, null) .scanFile (filename, mimetype)'은 파일의 색인을 생성해야합니다. – CommonsWare

+0

안녕하세요, 정말로 미안하지만, 당신은 내가 정말로 바보라고 생각해야합니다, 나는 단지 당신의 언급 된 코드를 어디에 넣을 지 모릅니다! 내가 이미 가지고있는 코드에서, 실제로 위에 무엇이 작동할까요? 미안하지만 고통이 될 수 있습니다. 이것은 모두 새로운 것입니다. 감사합니다. Lucy – Lucy

관련 문제