2017-04-19 21 views
1

我正在開發這個android應用程序,它從圖庫(在我的情況下是一個圖像)接收數據並顯示它。我還必須做的是爲我的應用程序創建一個SplashScreen。但是當我做我的意圖變爲空。Splashscreen和intents

清單的代碼:

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.xkbc1923.myapplication"> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <activity android:name=".SplashScreen"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".AlertExampleActivity"> 
     <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*" /> 
      <data android:mimeType="text/*" /> 
     </intent-filter> 
    </activity> 
</application> 

閃屏

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 

public class SplashScreen extends Activity { 
private static final int DISPLAY_DURATION = 1000; 
@Override 
protected final void onCreate(final Bundle savedInstState) { 
    super.onCreate(savedInstState); 
    setContentView(R.layout.activity_splashscreen); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class); 
      startActivity(i); 
      // close this activity 
      finish(); 
     } 
    }, DISPLAY_DURATION); 
} 
} 

中號ainActivity

import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.Uri; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.io.File; 
import static android.provider.CalendarContract.CalendarCache.URI; 
public class AlertExampleActivity extends AppCompatActivity { 
ImageView picView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    ///get the image view 
    //get the text view 
    setContentView(R.layout.activity_alert_example); 

    picView = (ImageView) findViewById(R.id.picture); 
    TextView txtView = (TextView) findViewById(R.id.txt); 

    if (txtView ==null) { 
     Log.w("Example", "TextView is null"); 
    } 

    //get the received intent 
    Intent receivedIntent = getIntent(); 
    //get the action 
    String receivedAction = receivedIntent.getAction(); 
    //find out what we are dealing with 
    String receivedType = receivedIntent.getType(); 
    //make sure it's an action and type we can handle 
    if (receivedAction.equals(Intent.ACTION_SEND)) { 
     Log.d("Example", "Send received"); 
     //content is being shared 
     if (receivedType.startsWith("text/")) { 
      Log.d("Example", "Text received"); 

      //handle sent text 
      //hide the other ui item 
      picView.setVisibility(View.GONE); 
      //get the received text 
      String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT); 
      //check we have a string 
      if (receivedText != null) { 
       //set the text 
       txtView.setText(receivedText); 
      } 
     } else if (receivedType.startsWith("image/")) { 
      Log.d("Example", "Image received"); 
      //handle sent image 
      handleSendImage(receivedIntent); 
     } 

    } else if (receivedAction.equals(Intent.ACTION_MAIN)) { 
     //app has been launched directly, not from share list 
     Log.d("Example", "Direct launch of App"); 
    } 
} 

private void handleSendImage(Intent intent) { 
    // Get the image URI from intent 
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    // When image URI is not null 
    if (imageUri != null) { 
     // Update UI to reflect image being shared 
     picView.setImageURI(imageUri); 
    } else{ 
     Toast.makeText(this, "Error occured, URI is invalid", Toast.LENGTH_LONG).show(); 
    } 
} 
} 
+0

你能寫詳細資料嗎?當你的意圖變爲空時?在哪些活動? –

+0

這裏是我的錯誤: 引起:java.lang.NullPointerException:嘗試在com.example的null對象引用 上調用虛擬方法'boolean java.lang.String.equals(java.lang.Object)'。 xkbc1923.myapplication.AlertExampleActivity.onCreate(AlertExampleActivity.java:48) –

+0

您沒有爲您的意圖添加任何額外的數據。所以它通常是空的。只需在SplashScreen.java上添加必要的數據(i)即可。 Ex - i.putExtra(「key」,「value」); i.setAction( 「動作」); i.setType( 「類型」); –

回答

0

你沒有任何額外的數據添加到你的意圖。所以它通常是空的。只需在SplashScreen.java上添加必要的數據到Intent(i)。例如 -

i.setAction("action"); 
i.setType("type"); 
i.putExtra("key", "value"); 
+0

我試過了:Intent i = new Intent(SplashScreen.this,AlertExampleActivity.class); i.setAction(Intent.ACTION_MAIN); i.setType(「image/*」); String [] mimetypes = {「image/*」,「video/*」}; i.putExtra(意圖。EXTRA_MIME_TYPES,mimetypes); startActivity(i); –

+0

但事情是,現在我不再收到圖像。 –

+0

請分享您的修改代碼。可能是ImageView對此不可見 - picView.setVisibility(View.GONE); –

0

我不知道這是否會解決你的問題,但你可以試試。

相反的:

Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class); 
      startActivity(i); 
      // close this activity 
      finish(); 

試試這個:

startActivity(new Intent(SplashScreen.this, AlertExampleActivity.class)); 
finish(); 
+0

你的編輯仍然沒有區別 – Raghunandan

+0

@Raghunandan我編輯了我的答案。 – Johny

+0

謝謝,但它仍然無法正常工作... –

0

我已經更改了清單文件,因爲我希望即使在外部調用它時也會顯示splashscreen。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".AlertExampleActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 



    </activity> 

    <activity android:name=".SplashScreen"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="image/*" /> 
      <data android:mimeType="text/*" /> 
     </intent-filter> 

    </activity> 

</application> 

,並改變了SplashscreenActivity:

public class SplashScreen extends Activity { 

private static final int DISPLAY_DURATION = 1000; 

@Override 
protected final void onCreate(final Bundle savedInstState) { 
    super.onCreate(savedInstState); 
    setContentView(R.layout.activity_splashscreen); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class); 

      i.setAction(Intent.ACTION_SEND); 
      i.setType("*/*"); 
      String[] mimetypes = {"image/*", "video/*"}; 
      i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); 
      startActivity(i); 

      // close this activity 
      finish(); 
     } 
    }, DISPLAY_DURATION); 
} 
} 

我還沒有在MainActivity改變任何東西,但現在我不再接收圖像...我是新來的Android所以我會很感激這個解釋。

+0

您能否向我展示您無法從MainActivity接收的圖像/對象?你是在談論這個 - String [] mimetypes = {「image/*」,「video/*」}; i.putExtra(Intent.EXTRA_MIME_TYPES,mimetypes); –

+0

事情是,因爲我將Action設置爲ACTION_SEND,即使當我直接啓動應用程序時,它也會給我一個SEND,而它應該是一個MAIN。 –