2014-10-22 34 views
0

我正嘗試訪問活動(如A)中正在捕獲的另一活動(稱爲B)中的圖像。現在在這裏,我有兩種選擇:將位圖作爲位數組發送到下一個活動

1. Save to sd card and then access in activity B through the filepath. 
    But, time taken to save is higher in some cases, probably because of higher 
    image size. 

2. Send the bit array through intent.putExtra("imageArray" , data) and access it 
    in activity B through getIntent(). But on surfing net, I found sending bigger 
    bitmap of 1MB or more is a bad practise but didn't find anything with regards to 
    bitarray. 

有人可以告訴我哪個選項更好嗎?併發送一個更大的bitArray位圖到另一個活動是一個不好的做法?

我的要求是:兩個活動之間的時間差A和B應該是最小的。此外,圖像應該立即加載到活動B中。

在此先感謝。

+1

將其添加到意圖之前將其轉換爲字節數組,然後將其發送出去並解碼。 – 2014-10-22 06:12:34

+1

http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android – 2014-10-22 06:12:52

+0

@AnikIslamAbhi它需要大量的時間來保存圖像SD卡,這是令人煩惱。 – vijay 2014-10-22 06:14:33

回答

1

你也可以在應用程序空間(單例)中使用全局變量。

例子:

public class YourApplication extends Application 
{ 
    private static YourApplication singleton; 
    Bitmap bitmap; // or any type, byte array 
    .... 

    public static YourApplication getInstance() 
    { 
     return singleton; 
    } 

} 
... 
在另一個類,你可以設置和獲取這個變量 '位圖'

YourApplication.getInstance().bitmap = ....; // in Activity A 

... = YourApplication.getInstance().bitmap; // in Activity B 

或內部的另一種方法,使用

....(..., YourApplication.getInstance().bitmap, ...); 
+0

似乎有幫助。謝謝@Tapa – vijay 2014-10-22 06:45:54

+0

請注意多線程情況下的單例變量 – 2014-10-22 06:56:36

1

如果您通過使用URL在活動A和活動B中加載圖像,則可以使用ion - https://github.com/koush/ion。它可以幫助您使用URL顯示圖片並緩存圖像,以便再次加載它會立即發生,只需將活動A中的網址發送到活動B即可。

如果您使用手機相機拍攝圖像,我會說那保存它是更好的方法,如果你想在未來一次發送很多圖片,第二個選項將會很糟糕。

+0

不,我捕獲活動A中的圖像並將其加載到活動B中。這裏緩存對我來說沒有多大意義,因爲相同的圖像不會再次加載。在活動A中,我有攝像頭集成,它將捕捉圖像,並且我需要在活動B中顯示該圖像。 – vijay 2014-10-22 06:23:01

+0

我不知道您使用的是什麼手機,但不使用相機在沒有您的幫助的情況下保存圖像?你如何稱呼你的意圖? Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(destination)); startActivityForResult(intent,REQUEST_IMAGE); – 2014-10-22 06:27:20

+0

我已經在我的應用程序中集成了攝像頭,在那裏我重寫了onPictureTaken(args),因爲我需要附加一個貼紙到捕獲的圖像。現在,我調用本地函數saveImageToExternalStorage()來保存圖像。但它有時需要。我想用intent.putExtra(「imageArray」,finalImage)替換它,它會是一個好方法嗎?我需要該應用在高清設備(如銀河S5,Nexus 4,5等)中運行良好,圖像尺寸要高得多。 – vijay 2014-10-22 06:33:08

相關問題