2012-01-05 237 views
1

我已將圖像保存在內部存儲器中。例如,我將它保存爲test.jpg。現在我想在imageview中顯示它。我曾經嘗試這樣做:從Android的內部存儲器讀取圖像

ImageView img = (ImageView)findViewById(R.id.imageView1); 
try {      
    img.setImageDrawable(Drawable.createFromPath("test.jpg"));     
} catch (Exception e) { 
    Log.e(e.toString()); 
} 

並獲得logcat的以下信息:

SD卡可用於讀取和寫入truetrue

任何幫助或引用嗎?

回答

5
public class AndroidBitmap extends Activity { 

private final String imageInSD = "/sdcard/er.PNG"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Bitmap bitmap = BitmapFactory.decodeFile(imageInSD); 
     ImageView myImageView = (ImageView)findViewById(R.id.imageview); 
     myImageView.setImageBitmap(bitmap); 

    } 
} 

final Bitmap bm = BitmapFactory.decodeStream(..some image..); 
// The openfileOutput() method creates a file on the phone/internal storage in the context of your application 
final FileOutputStream fos = openFileOutput("my_new_image.jpg", Context.MODE_PRIVATE); // Use the compress method on the BitMap object to write image to the OutputStream 
bm.compress(CompressFormat.JPEG, 90, fos); 

希望工程......

+0

謝謝,但我沒有在SD卡中保存圖像,使用** MODE_PRIVATE ** .any的想法保存圖像到內部存儲器中以獲取內部存儲器的路徑名。 – 2012-01-05 06:05:21

+1

final Bitmap bm = BitmapFactory.decodeStream(..some image ..); // openfileOutput()方法在應用程序上下文中在手機/內部存儲上創建一個文件 final FileOutputStream fos = openFileOutput(「my_new_image.jpg」,Context.MODE_PRIVATE); //使用BitMap對象上的compress方法將圖像寫入OutputStream中bm.compress(CompressFormat.JPEG,90,fos); – 2012-01-05 06:09:18

+0

Thanx @Nikhilreddy Gujjula – 2012-01-05 06:11:19

0

看看在doc ..

你並不需要 「知道」 裏的形象存儲。你只需要堅持你保存的內容,然後你可以再次閱讀它。

問題是......你在那裏保存文件?或者你是否將這些圖像與編入應用程序的圖像混淆?如果是後者,文檔還會討論如何讀取這些文件,它們與您寫入設備的文件不同,它們是應用程序包的一部分。

歡迎來到Stack!如果您發現有用的答案,請不要忘記加註他們,並在他們解決您的問題時將其標記爲「正確」。

乾杯。

+0

謝謝Dr.Dredel – 2012-01-05 07:08:01

+0

不客氣:) – 2012-01-05 07:35:09

相關問題